Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL check if query returns empty

Tags:

plsql

I'm writing a procedure, and i need to check whether my select query returned an empty record or not. (In this example whether there is no x,y shelf)

How can i do that?

I tried this:

temp shelves.loadability%TYPE := NULL; BEGIN  select loadability into temp from shelves where rownumber = x and columnnumber = y; IF temp IS NOT NULL THEN /* do something when it's not empty */ ELSE /* do the other thing when it's empty */ END IF; 

But the second branch of the if never works...

EDIT:

Oh, it was so easy...

temp shelves.loadability%TYPE; BEGIN  select count(*) into temp from shelves where rownumber = x and columnnumber = y; IF temp != 0 THEN /* do something when it's not empty */ ELSE /* do the other thing when it's empty */ END IF;  END; 
like image 805
WonderCsabo Avatar asked May 10 '11 19:05

WonderCsabo


People also ask

Is empty in Plsql?

The IS EMPTY function provides an alternative syntax to IS_EMPTY and also returns TRUE if that set is empty. where set is a set of any set data type, such as a multi-assign double attribute. The results of this example would the same as the previous IS_EMPTY example.

Is empty in Oracle SQL?

Use the IS [NOT] EMPTY conditions to test whether a specified nested table is empty, regardless whether any elements of the collection are NULL . The condition returns a boolean value: TRUE for an IS EMPTY condition if the collection is empty, and TRUE for an IS NOT EMPTY condition if the collection is not empty.

Is null in Oracle SQL query?

Introduction to the Oracle IS NULL operatorIt is a marker for missing information or the information is not applicable. NULL is special in the sense that it is not a value like a number, character string, or datetime, therefore, you cannot compare it with any other values like zero (0) or an empty string (”).

How do I check if a variable is null in PL SQL?

It takes any number of arguments, and returns the first one which is not NULL. If all the arguments passed to COALESCE are NULL, it returns NULL. In contrast to NVL , COALESCE only evaluates arguments if it must, while NVL evaluates both of its arguments and then determines if the first one is NULL, etc.


1 Answers

Use an exception handler

Begin   select column   into variable   from table   where ...;    -- Do something with your variable  exception  when no_data_found then     -- Your query returned no rows --   when too_many_rows     -- Your query returned more than 1 row --  end; 
like image 87
Rene Avatar answered Sep 25 '22 00:09

Rene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!