Subject weird result from a SP
Author duilio_fos
using FB v.1.0, I have data stored in 2 master-slave tables:

select * from dty_hd

ID_DTY FM_DEPOT TO_DEPOT
=========== =========== ===========

105 16 16
106 16 16
108 16 16

select * from dty_li

ID_DTY ID_LIN T1 T2
=========== =========== =================== ======================

105 111 0.3298611111111111 0.375
106 112 0.3263888888888889 0.3569444444444444


I wrote the following code:

create procedure FstLin(id_dty integer)
returns
(result integer)
as
begin
select first 1 id_lin from dty_li a
where id_dty=:id_dty
order by t1
into :result;
suspend;
end !!

create procedure LstLin(id_dty integer)
returns
(result integer)
as
begin
select first 1 id_lin from dty_li a
where id_dty=:id_dty
order by t2 desc
into :result;
suspend;
end !!

create procedure LstDTY_HD
returns
(id_dty integer, fm_depot integer, fm_time double precision,
to_time double precision )
as
declare variable RetCode integer;
declare variable Id integer;
begin
for select id_dty, fm_depot from dty_hd
into :id_dty, :fm_depot
do
begin

fm_time=null;
to_time=null;

select t1 from dty_li b
where b.id_dty=:id_dty
and b.id_lin=(select result from FstLin(:id_dty))
into :fm_time;

select result from LstLin(:id_dty) into :id;

select t2 from dty_li c
where c.id_dty=:id_dty
and c.id_lin=(select result from LstLin(:id_dty))
into :to_time;

suspend;
end
end !!

issuing the command

select * from LstDty_hd

I was expecting

ID_DTY FM_DEPOT FM_TIME TO_TIME
=========== ======== ====================== ======================

105 16 0.3298611111111111 0.375
106 16 0.3263888888888889 0.3569444444444444
108 16 <null> <null>


instead, I got

ID_DTY FM_DEPOT FM_TIME TO_TIME
=========== ======== ====================== ======================

105 16 0.3298611111111111 0.375
106 16 <null> <null>
108 16 <null> <null>


I rewrote the code like this

create procedure LstDTY_HD
returns
(id_dty integer, fm_depot integer, fm_time double precision,
to_time double precision )
as
declare variable RetCode integer;
declare variable Id integer;
begin
for select id_dty, fm_depot from dty_hd
into :id_dty, :fm_depot
do
begin

fm_time=null;
to_time=null;

select result from FstLin(:id_dty) into :id;

select t1 from dty_li b
where b.id_dty=:id_dty
and b.id_lin=:id into :fm_time;

select result from LstLin(:id_dty) into :id;

select t2 from dty_li c
where c.id_dty=:id_dty
and c.id_lin=:id into :to_time;

suspend;
end
end !!

and this time it worked as expected.

Can somebody explain why ?

TIA

Duilio Foschi