Subject Re: [firebird-support] Strange SP behaviour
Author Ivan Prenosil
> ALTER PROCEDURE CREATE_C_ENTRY (VA_ID VARCHAR(10))
> RETURNS (RC_ID INTEGER,
> RA_ID VARCHAR(10),
> RE_CODE VARCHAR(10))
> AS
>
> declare variable vE_CODE varchar(10);
> declare variable vC_ID integer;
> BEGIN
> for select e_code from table_a where id = :vA_ID into :vE_CODE do begin
> select c_id from table_c where a_id = :vA_ID and e_code = :vE_CODE into :vC_ID;
>
> if (vC_ID is null) then begin
> vC_ID = gen_id(gen_een,1);
> insert into table_c(c_id,a_id,e_code) values (:vC_ID,:vA_ID,:vE_CODE);
> end else begin
> update table_c set
> c_id = :vC_ID,a_id = :vA_ID,e_code=:vE_CODE
> where c_id = :vC_ID;
> end
> rC_ID = :vC_ID;
> rE_CODE = :vE_CODE;
> rA_ID = :vA_ID;
> suspend;
> end
> END

I think you forgot to initialize vC_ID before each iteration, i.e.

for select e_code from table_a where id = :vA_ID into :vE_CODE do begin
vC_ID = NULL;
select c_id from table_c where a_id = :vA_ID and e_code = :vE_CODE into :vC_ID;


Ivan