Subject RE: Re[4]: [firebird-support] Join with first detail record
Author Alan McDonald
> My SP looks like this:
>
> CREATE PROCEDURE SP_TEST1 (st_id bigint)
> returns (m_id bigint, m_field1 bigint, m_field2 bigint,
> m_field3 integer, m_field4 integer,
> d_id bigint, d_field1 integer, d_field2 integer)
> AS
> BEGIN
> FOR SELECT m.id, m.field1, m.field2, m.field3, m.field4
> FROM mastertable m
> WHERE m.id=:st_id
> INTO :m_id, :m_field1, :m_field2, :m_field3, :m_field4
> DO
> BEGIN
> FOR SELECT first 1 id, field1, field2
> FROM detailtable d
> WHERE d.masterid=:m_id
> ORDER BY field2
> INTO :d_id, :d_field1, :d_field2
> DO
> BEGIN
> suspend;
> END
> END
> END!!
>

BTW - if you only ever want one record from this then

> CREATE PROCEDURE SP_TEST1 (st_id bigint)
> returns (m_id bigint, m_field1 bigint, m_field2 bigint,
> m_field3 integer, m_field4 integer,
> d_id bigint, d_field1 integer, d_field2 integer)
> AS
> BEGIN
> SELECT m.id, m.field1, m.field2, m.field3, m.field4
> FROM mastertable m
> WHERE m.id=:st_id
> INTO :m_id, :m_field1, :m_field2, :m_field3, :m_field4
> SELECT first 1 id, field1, field2
> FROM detailtable d
> WHERE d.masterid=:m_id
> ORDER BY field2
> INTO :d_id, :d_field1, :d_field2
suspend;
> END!!

will do it - no need for 2 for loops

Alan