Subject Re: [firebird-support] Derived Tables in PSQL
Author jft
Rade,
Realised later you may be looking for "for select ..." syntax.
Yes, it works too:
/* */
/* illustrates derived tables (including joins thereon) */
/* in procedures in FB2 using "for select ..." syntax */
create table TEST1(Int1 int, Int2 int);
create table TEST2(Int1 int, Int2 int);
create table TEST3(Int1 int, Int2 int);
commit;

set term ^;
recreate procedure TEST_DERIVED
as
declare intX int;
declare intY int;
declare intZ int;
begin

insert into TEST1(Int1,Int2)
select x,y from (select 1,111 from RDB$DATABASE) as DT1(x,y);
insert into TEST1(Int1,Int2)
select x,y from (select 2,222 from RDB$DATABASE) as DT1(x,y);

insert into TEST2(Int1,Int2)
select x,y from (select 1,1000 from RDB$DATABASE) as DT2(x,y);
insert into TEST2(Int1,Int2)
select x,y from (select 2,2000 from RDB$DATABASE) as DT2(x,y);

/* "for select ..." with join on derived table as well */
for select DT3.x,DT3.y, DT4.y
from (select Int1,Int2 from TEST1) as DT3(x,y)
join (select Int1,Int2 from TEST2) as DT4(x,y)
on DT3.x = DT4.x
into :intX, :intY, :intZ
do
begin
insert into TEST3 (Int1, Int2)
values (:intX, :intY + :intZ);
end

end^
set term ;^
commit;

execute procedure TEST_DERIVED;
commit;

select Int1,Int2 from TEST3 /* returns 1,1111 and 2, 2222 */;
commit;

drop procedure TEST_DERIVED;
commit;
drop table TEST1;
drop table TEST2;
drop table TEST3;
commit;
/* */

HTH,
John

> -------Original Message-------
> From: radevojvodic <vrade@...>
> Subject: [firebird-support] Derived Tables in PSQL
> Sent: 29 Mar '07 04:05
>
> Hi All,
>
> Is there any possibility of using Derived Tables feature of Firebird 2
> in PSQL (stored procedure returns an error when i try to make for
> select statement with derived table join included)
>
> Rade
>
>