I encountered the following PL/SQL code(variables modified) at work:
PROCEDURE test(i_w IN a.w%type,o_result IN OUT resu_cur_type) IS
BEGIN
IF i_w IS NULL THEN
open o_result for SELECT a.x, b.y FROM a,b WHERE a.z=b.z;
ELSE
open o_result for SELECT a.x, b.y FROM a,b WHERE a.z=b.z AND a.w=i_w;
END IF;
END test;
I think the above is equivalent to the following:
PROCEDURE test(i_w IN a.w%type,o_result IN OUT resu_cur_type) IS
BEGIN
open o_result for SELECT a.x, b.y FROM a,b WHERE a.z=b.z AND NVL(a.w,1)=NVL(NVL(i_w,a.w),1);
END test;
Won’t it be the same?. Could somebody please explain is there any reason to use the
original version?.
Please note that the original cursor is a 50 line query, so refactoring might improve its readability and keep it simple.
Edit:
To address the problem in Kevin Burton’s answer, I’ve updated the WHERE condition.
if a.w is null you will not select the records
as
will be