我需要查询一个表,并且可以根据前端选择的选项在 WHERE 子句中传递一个或最多六个参数。我在 WHERE 子句中使用了 CASE 语句来处理所有排列。
这是 SP 代码片段:
create procedure return_data (
p_field1 in varchar(20),
p_field2 in varchar2(30),
p_field3 in varchar2(30),
cur out sys_refcursor)
is
begin
open cur for
select col1, col2, col3,col4,col5,col6
from master_table
where (case when (p_field1 is null) then (1)
when (p_field1 is not null) and (col1=p_field1) then 1
else 0 end) =1
and (case when (p_field2 is null) then (1)
when (p_field2 is not null) and (col2=p_field2) then 1
else 0 end) =1
... …Run Code Online (Sandbox Code Playgroud)