Subject RE: [firebird-support] exists() in stored procedure with ordered
Author Leyne, Sean
Christian,

> the following query works in ISQL with firebird 2.1.2:
>
> select animalid
> from PROCGETACTIVEANIMALS( :reportdate )
> where not exists(
> select tav.UIDANIMAL, tav.LOGTIMESTAMP
> from TBLANIMALDAYREPORT tav
> where (tav.UIDANIMAL = animalid ) and (tav.KIND = 'R')
> order by tav.LOGTIMESTAMP
> )

While it does "work", it is poor SQL.

You need to re-think how EXISTS operates -- it simply checks if any record matches the criteria.

So don't think of it as a standand alone query which returns a meaningful or ordered result set, but rather a basic query to define a criteria.

The more appropriate SQL statement would be

select animalid
from PROCGETACTIVEANIMALS( :reportdate )
where not exists(
select 1
from TBLANIMALDAYREPORT tav
where (tav.UIDANIMAL = animalid) and (tav.KIND = 'R')
)

Sean