Subject Re: [firebird-support] Problems with cursor
Author Helen Borrie
At 12:54 PM 9/03/2005 +0100, you wrote:

>Hello again,
>
>I still have the same problems with my cursor.
>
>For my understanding, is this a proper solution?
>
>First I fill the cursor by selecting (FOR select ... INTO ... DO begin
>... end) values into the cursor.
>Next I give ids (still in first "DO begin ... end") into 3 different
>cursors (one after another), which insert/update the same table.

No. Create local variable for the columns in the new rows. Finally,
insert once in the "primary" table and once into the "secondary"
table. (Code outline below).

>If I specify the customer in the first select, this proc works fine. As
>soon as I want to select any customer nothing happens.

This procedure isn't a selectable procedure, it's an executable one.


>May I need a SUSPEND somewhere?

No, only for executable procedures. Don't even think of making this
procedure selectable. You'll have issues with the uncommitted work from
the SP being made to look as if it is committed data. Run and commit the
executable procedure first; then commit it; then select from the tables.

declare variable v_id integer;
declare variable f1 varchar(10);
declare variable f2 char;
declare variable f3 numeric(18,2);
declare variable calc_f1 varchar(10);
declare variable calc_f2 char;
declare variable calc_f3 numeric(18,2);

begin // selecting all affected customers
for
select ID
from tab2
where ...
into :V_ID
DO
begin // do every step for each customer
select f1, f2
from tab3
where ID = :V_ID
into :f1, :f2;
for
select f3
from tab4
where ID = :V_ID
into :f3
DO
begin
insert into tab1 (v_id, f1, f2, f3)
values (:v_id, :f1, :f2, :f3);

/* note that the FOR loop here potentially will cause you have multiple
inserts of both kinds */

/* Now do your calculations on the variables, but watch out that you don't
change v_id -- and make sure that the vars are properly initialised each
time */
calc_f1 = <whatever>;
calc_f2 = <whatever>;
calc_f3 = <whatever>;

insert into tab1 (v_id, f1, f2, f3)
values (:v_id, :calc_f1, :calc_f2, :calc_f3)'
end
end
end

./hb