求教一个问题,我要写一个存储过程,根据很多不同的参数查询,因为不确定传入的哪些参数是空的,但要根据非空的参数查询,这样就有太多不同的组合,有什么好的办法吗

解决方案 »

  1.   

    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;
      

  2.   

    也可以这么写:
    select * from t 
    where (col1 = p1 or p1 is null) 
      and (col2 = p2 or p2 is null)
    p1,p2是传递的参数