create or replace procedure ........ declare v_sql varcha2(2000); begin v_sql := 'select * from t where 1=1 '; if 条件1 is not null then v_sql :=v_sql || ' and col1=条件1'; end if; if 条件2 then ................ end if; open cur for v_sql; end;
也可以这么写: select * from t where (col1 = p1 or p1 is null) and (col2 = p2 or p2 is null) p1,p2是传递的参数
declare
v_sql varcha2(2000);
begin
v_sql := 'select * from t where 1=1 ';
if 条件1 is not null then
v_sql :=v_sql || ' and col1=条件1';
end if;
if 条件2 then
................
end if; open cur for v_sql;
end;
select * from t
where (col1 = p1 or p1 is null)
and (col2 = p2 or p2 is null)
p1,p2是传递的参数