How to use WITH Clause in the oracle forms - oracle10g

How to use WITH Clause in the oracle forms 10.1.2.0.2 with 10g database?
In the Button
Please help me

What do you mean by "In the Button" in your question?
In general, WITH clause can be used as follows :
with t as(select 1 from dual union select 2 from dual)
select * from t;
In your oracle forms button trigger : the equivalent of the above query can be as follows :
declare
cursor c1 is select * from (select 1 from dual union select 2 from dual);
begin
--your statements
end;

EX:
select employee_id, last_name, job_id, manager_id
from employees
START WITH employee_id = 101
CONNECT BY PRIOR manager_id = employee_id ;

Related

Postgres - SELECT FOR UPDATE with union all

I am trying to put row level lock on a table in one postgres function.
do $$
declare tabname text :='locktest' ;
begin
execute 'create temp table temp1 as
select v.* from (
select row_number() over (partition by a.id) as row_num,a.*
from '||tabname||' a,locktest2 b where a.id=b.id and b.val=111
union all
select row_number() over (partition by a.id) as row_num,a.*
from '||tabname||' a,locktest2 b where a.id=b.id and b.val=222
)v where v.row_num=1 for update';
raise notice 'Completed';
end $$;
But while compiling it , getting below error.
ERROR: FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT
Please suggest.
The way the statement is written, the database does not know in which table to lock the rows.
Rewrite the query along these lines:
SELECT ... FROM
(SELECT ...
FROM tab1
WHERE ...
FOR NO KEY UPDATE) AS t1
UNION ALL
(SELECT ...
FROM tab2
WHERE ...
FOR NO KEY UPDATE) AS t2;
FOR NO KEY UPDATE is the correct lock if you plan to update. FOR UPDATE is the lock if you intend to delete.

Apply MAX(LENGTH) on all columns of a table

I want to check the MAX(LENGTH) of all VARCHAR columns of my Redshift table, so I have an idea of a better limitation I could put on the column sizes. (Right now all columns are VARCHAR(65535))
I can write a query like:
SELECT MAX(LENGTH(col1)), MAX(LENGTH(col2)), ...
FROM my_table
But isn't there a way I could write my query so it basically says "apply this for every column"? I've tried the answer from this post but it seems it only applies to classic PostgreSQL
You can use the following SQL the generate your select
select sql from (
select 1 o, 'select ' sql
union
select 2, 'max(length('+attname+')),'
from pg_class c
join pg_attribute a on c.oid = a.attrelid
where relname = '<your_table>'
and attnum > 0
union
select 3, 'from <your_table>'
)
order by o
The output will look like this
select
max(length(col1)),
max(length(col2)),
...
max(length(coln)), -- <- remove the last comma
from <your_table>
You can run this sql to get all max lengths from your table
Please let me know if this helps you.

Sum of a column within the subquery in Postgresql

I have a Postgresql table where I have 2 fields i.e. ID and Name ie column1 and column2 in the SQLFiddle. The default record_count I put for a particular ID is 1. I want to get the record_count for column 1 and sum that record_count by column1.
I tried to use this query but somehow its showing some error.
select sum(column_record) group by column_record ,
* from (select column1,1::int4 as column_record from test) a
Also find the Input/Output screenshot in the form of excel below :
SQL Fiddle for the same :
http://sqlfiddle.com/#!15/12fe9/1
If you're using a window function (you may want to use normal grouping, which is "a lot" more faster and performant), this is the way to do it:
-- create temp table test as (select * from (values ('a', 'b'), ('c', 'd')) a(column1, column2));
select sum(column_record) over (partition by column_record),
* from (select column1, 1::int4 as column_record from test) a;

insert into temp table without creating it from union results

I have the below query that get results from more than one select.
Now I want these to be in a temp table.
Is there any way to insert these into a temp table without creating the table?
I know how to do that for select
Select * into #s --like that
However how to do that one more than one select?
SELECT Ori.[GeoBoundaryAssId], Ori.[FromGeoBoundaryId], Ori.Sort
From [GeoBoundaryAss] As Ori where Ori.[FromGeoBoundaryId] = (select distinct [FromGeoBoundaryId] from inserted )
Union
SELECT I.[GeoBoundaryAssId], I.[FromGeoBoundaryId], I.Sort
From [inserted] I ;
Add INTO after the first SELECT.
SELECT Ori.[GeoBoundaryAssId], Ori.[FromGeoBoundaryId], Ori.Sort
INTO #s
From [GeoBoundaryAss] As Ori where Ori.[FromGeoBoundaryId] = (select distinct [FromGeoBoundaryId] from inserted )
Union
SELECT I.[GeoBoundaryAssId], I.[FromGeoBoundaryId], I.Sort
From [inserted] I ;
Try this,
INSERT INTO #s ([GeoBoundaryAssId], [FromGeoBoundaryId], Sort)
(
SELECT Ori.[GeoBoundaryAssId], Ori.[FromGeoBoundaryId], Ori.Sort
FROM [GeoBoundaryAss] AS Ori WHERE Ori.[FromGeoBoundaryId] in (SELECT DISTINCT [FromGeoBoundaryId] FROM inserted )
UNION
SELECT I.[GeoBoundaryAssId], I.[FromGeoBoundaryId], I.Sort
FROM [inserted] I
)

How to make derived column in Oracle and then use it?

How I can make/declare/define a derived column in select query and then use it in where clause?
To define a column in an SQL query, you can use pretty much any SQL operation that returns a single value (including select statements). Here are some examples:
select 'Y' from dual;
select (5 * 3) cal_col from dual;
select (select min(col1) from table 2) calc_col from dual;
select nvl(col1, 'N') has_value from mytable;
From my experience, if you want to use a derived column in a select query, then you must define the column as part of an inner select. Here is an example:
select *
from (
select (col1 * col2) calc_col
from mytable
) data
where data.calc_col > 30
Another alternative is use the calculation within the where clause itself:
select (col1 * col2) calc_col
from mytable t
where (col1 * col2) > 30
If you are performing a count(*) operation, then you can also leverage the HAVING clause:
select field1, count(*)
from mytable
having count(*) > 3