DotNetFirebird.org DotNetFirebird
Using Firebird SQL in .NET.
Tuesday, November 21, 2006

Checking if a record exists in Firebird (EXISTS function)

If you need to check whether certain record exists the usual way is to execute the following command and then check if the :idexists variable is greater than zero (this is an example from a stored procedure:

SELECT count(*) FROM company WHERE companyid = :id INTO :idexists;

But the right way to do this is to use EXISTS() function. If you use COUNT(*) Firebird will be looking for all item that fit the condition (i.e. companyid = :id).

If you use EXISTS() it will stop when it finds the first one - so it's much faster!

idexists = 0;
SELECT 1 FROM rdb$database WHERE EXISTS(SELECT * FROM company WHERE companyid = :id) INTO :idexists;

Comments:

Blog comments are closed.



Previous

Archives