Tuesday, May 21, 2013

oracle query to return null when query returns no rows to return

Assume there is employee table (oracle 11g)

employee
empolyee_num   employeeName  department

10001                 ABC1                Sales
10002                ABC2                Accounts
10003                ABC3                Sales


employee_salary (employee# is foreign key)

employee_sal_id   employee_num  employee_salary
           1                   10001              1000
           2                  10002                1200

the below query

Query #1:
select es.employee_sal from employee emp, employee_salary es where emp.employee_num = es.employee_num
and emp.employee_num = 10003;

the above query will return no rows returned.

If the above query needs to return a null we can wrap the above query as below

Query #2:
select (select es.employee_sal from employee emp, employee_salary es where emp.employee_num = es.employee_num and emp.employee_num = 1000) as salary from dual;

this query will return a null instead of no row found.

If needed we can return a 0 instead of null using the nvl () function.

Query #3:
select nvl((select es.employee_salary from employee emp, employee_salary es where emp.employee_num = es.employee_num and emp.employee_num = 1000),0) as salary from dual;

Note:
There might be scenarios while working with procedures or function, if the WHERE clause condition didn't match the query possibly returns NO ROW FOUND and if i use the if clause to check for NULL the PL/SQL block would throw an error.

In my case,  i wanted to find the salary for the employee writing a procedure.
 in cursor i fetched the employee_num, then used this empolyee_num to a select query to get the salary and print in DBMS_OUT.

Create table using the below queries in ORACLE 11g


CREATE TABLE employee
(
   employee_num    NUMBER (10) CONSTRAINT pk_emp_num PRIMARY KEY,
   employee_name   VARCHAR2 (25),
   department      VARCHAR2 (20)
);




CREATE TABLE employee_salary
(
   employee_sal_id   NUMBER (10) CONSTRAINT pk_emp_sal_id PRIMARY KEY,
   employee_num      NUMBER (10),
   employee_sal      NUMBER (6, 2),
   CONSTRAINT fk_supplier FOREIGN KEY
      (employee_num)
       REFERENCES employee (employee_num)
);

Insert the values into the tables using below query:

insert into employee values(10001,'ABC1','Sales');
insert into employee values(10002,'ABC2','Accounts');
insert into employee values(10003,'ABC3','Sales');

insert into employee_salary values (1, 10001, 1000.00);
insert into employee_salary values (2, 10002, 1200.00);

Try executing the below PL/SQL block code:


DECLARE
   CURSOR cur_employee_num
   IS
      SELECT *
        FROM employee emp;

   curval1   cur_employee_num%ROWTYPE;
   salval    NUMBER (6, 2) := 0;
BEGIN
   OPEN cur_employee_num;

   LOOP
      FETCH cur_employee_num INTO curval1;

      EXIT WHEN cur_employee_num%NOTFOUND;

      IF curval1.employee_num IS NOT NULL AND curval1.employee_num > 0
      THEN
         SELECT employee_sal
           INTO salval
           FROM employee_salary
          WHERE employee_num = curval1.employee_num;

         IF salval > 0
         THEN
            DBMS_OUTPUT.put_line (
                  'Salary of employee '
               || curval1.employee_num
               || ' is = '
               || salval);
         END IF;
      END IF;
   END LOOP;

   CLOSE cur_employee_num;
END;
/


OUTPUT:
Error or exception:
ORA-01403: no data found

ORA-06512: at line 19

Salary of employee 10001 is = 1000
Salary of employee 10002 is = 1200



If i change the above PL/SQL block as below (refer the highlighted code below)

DECLARE
   CURSOR cur_employee_num
   IS
      SELECT *
        FROM employee emp;

   curval1   cur_employee_num%ROWTYPE;
   salval    NUMBER (6, 2) := 0;
BEGIN
   OPEN cur_employee_num;

   LOOP
      FETCH cur_employee_num INTO curval1;

      EXIT WHEN cur_employee_num%NOTFOUND;

      IF curval1.employee_num IS NOT NULL AND curval1.employee_num > 0
      THEN
         SELECT (SELECT employee_sal
                   FROM employee_salary
                  WHERE employee_num = curval1.employee_num)
           INTO salval
           FROM DUAL;

         IF salval > 0
         THEN
            DBMS_OUTPUT.put_line (
                  'Salary of employee '
               || curval1.employee_num
               || ' is = '
               || salval);
         END IF;
      END IF;
   END LOOP;

   CLOSE cur_employee_num;
END;
/

OUTPUT:

Salary of employee 10001 is = 1000
Salary of employee 10002 is = 1200
(there is no exception)

Query #3 can be used if a 0 to be returned instead of NULL in the highlighted query.




No comments:

Post a Comment