Subject Re: [ib-support] SELECT first
Author Helen Borrie
At 04:48 PM 9/06/2003 -0700, you wrote:
>What is the fastest way to find the first matching record for a select
>query without having to count all of them. I just want to know if even a
>single match exists. My understanding is that COUNT has to continue
>through the entire table.

Assuming you are using Firebird ---

If you want to fetch the data of the first matching record, you'll need to
include an ORDER BY clause to determine what "first" means.

SELECT FIRST 1 <column-list>
FROM <table>
ORDER BY <ordering-column>

If you only want to test whether any matching row exists in a different
table, then use a subquery with the WHERE EXISTS( ) predicate:

SELECT <column-list>
FROM <this-table>
WHERE EXISTS (
SELECT 1 FROM <that-table>
WHERE <search-criteria-for-that-table>)

In a stored procedure or trigger, you can also test using
IF (EXISTS (SELECT... ))

heLen