Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pl/sql - Is there a NVL() for empty rows

Tags:

oracle

plsql

With this,

set serveroutput on size 900000;
DECLARE
  test VARCHAR(255):=0;
BEGIN
    SELECT id INTO test FROM sku WHERE id = 1515;
    dbms_output.put_line('Result:' || test);
END;

I'm having the error

"no data found"

When ID doesn't exist in database.

How can I use something like nvl() here, so I can get a default value instead of get an error?

like image 541
Topera Avatar asked Oct 08 '10 15:10

Topera


1 Answers

If appropriate you could do this as an alternative to the more usual NO_DATA_FOUND exception handling:

DECLARE
  test VARCHAR(255):=0;
BEGIN
    SELECT NVL(MAX(id),'default') INTO test FROM sku WHERE id = 1515;
    dbms_output.put_line('Result:' || test);
END;

By using MAX(ID) you are sure to get one row back, which will have a NULL when there is no data; you then use NVL in the usual way.

like image 51
Tony Andrews Avatar answered Dec 22 '22 08:12

Tony Andrews