Generate the dynamic where condition in pl/sql cursor based on input values
For Example :
Input Values : a b c values
Query Format :
CREATE OR REPLACE Procedure abcprocedure ( a IN Number,b IN Number, c IN Number ) cursor abccursor IS select 1 from pqr p where ( Prepare the where condition based on a,b,c values and null checks also )
Advertisement
Answer
This is how I understood the problem; no dynamic stuff is necessary.
For simpler testing (on my side, as I don’t have your tables), on Scott’s DEPT
table, this might do the job (par_x
is name of procedure’s parameter).
select * from dept where deptno in (par_a, par_b, par_c) or (par_a is null and par_b is null and par_c is null);
If those are different columns, no problem either:
select * from dept where (deptno = par_a or par_a is null) and (dname = par_b or par_b is null) and (loc = par_c or par_c is null);
If certain parameter isn’t passed and you don’t want to see that column’s value, then use
select case when par_a is not null then deptno else null end deptno, -- case when par_b is not null then dname else null end dname, -- case when par_c is not null then loc else null end loc from dept where (deptno = par_a or par_a is null) and (dname = par_b or par_b is null) and (loc = par_c or par_c is null);
If you want to exclude the column from result set, well, that isn’t that simple. If you used (for example) Oracle Apex, you could choose not to render that column. Otherwise, in pure SQL, I wouldn’t know how to do that.