Subject Fastest way to check for record
Author paulfilmer
Hi All

Often I need to test whether a record exists. For example, checking
whether a product has been sold in the last X days may have SQL that
looks like:

SELECT KEYFIELD
FROM SALESDATA
WHERE (PRODUCT = :someproduct) AND
(TRANSACTION_DATE >= :somedate)

Assume there could be 1000's of matching records, but I just want to
know if at least one exists. The SQL could also perform joins across
tables. If cursor is in a tight loop in the code (eg checking
10,000's of products) then I want to optimise such code for best
response.

The best way I have found to do this is a TIB_Cursor, setting MaxRows
set to 1. I could also use 'SELECT COUNT(*)' but that would have to
get all records... I just want to know if at least one exists. As far
as I know you can't use an 'IF EXISTS()' in this situation.

Can anyone suggest a faster way to do such a query using IBO
components? I don't know much about stored procedures. Do you think
that could help?

Thanks very much in advance!

Paul.