Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL : problem with if statement
Astrid Ritscher wrote:
>
> Hi,
> I would like to know if there is any possibility to code in PSQL
> something like this (from Sybase Transact SQL):
>
> IF EXISTS (SELECT k_num FROM kunde)
> BEGIN
> ...
> END
>
> which tests for the existece of a certain row. This is faster than
> counting the number of rows because EXISTS stops the select after
> having found the first row.
>
> Thank you for any hints,
> Astrid.
>
> --
> Astrid Ritscher, ISL, Bremen, Germany
When testing for this type of condition you should always declare an
explicit cursor, fetch one row to check for existence, then close the
cursor.
Example:
DECLARE
CURSOR c_exists IS
SELECT k_num FROM kunde;
krec c_exists%ROWTYPE;
BEGIN
OPEN c_exists;
FETCH c_exists INTO krec;
IF c_exists%FOUND THEN
...
END IF;
CLOSE c_exists;
END;
Note that this is better than the following:
DECLARE
l_num kunde.k_num%TYPE;
BEGIN
SELECT k_num INTO l_num FROM kunde;
Chris Halioris
Tactics, Inc.
Received on Wed Aug 13 1997 - 00:00:00 CDT