Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: Q: select with set operators and brackets
Hi!
Gombos Bertalan wrote:
> Hi all!
>
> I would like to issue the following SELECT in PL/SQL:
> (SELECT 1 FROM dual MINUS
> SELECT 2 FROM dual)
> UNION
> (SELECT 3 FROM dual MINUS
> SELECT 4 FROM dual)
>
> How I can do it? It works in SQL but (SELECT 1 INTO x ... ) doesn't work in
> PL/SQL.
>
> Thanks a lot,
> --
> G o m b o s B e r t a l a n
> system engineer, Oracle developer
> mailto:bgombos_at_freemail.c3.hu
First, if you use (SELECT 1 INTO x ... ) you'll try to fetch multiple rows into a single variable. That won't work at all in pl/sql. So you are in need of cursor.
To use a cursor in your example, you'll need an alias in the first selection of the query to name column. If you use table colums in the query, the alias might not be necessary.
So it should look like this:
begin
declare
cursor cur_a is
(SELECT 1 alias_n FROM dual MINUS
SELECT 2 FROM dual)
UNION
(SELECT 3 FROM dual MINUS
SELECT 4 FROM dual);
begin
for row_a in cur_a loop
...
end loop;
end;
end;
/
--
Jörn Fieg WMD GmbH, Hamburg /Germany