begin
  if parm2 is not null 
   select * from tab
  where tab.col1=parm1
  and tab.col2=parm2  
  ;
  else 
   select * from tab
  where tab.col1=parm1;  
  
end;

解决方案 »

  1.   

    这样写是可以拉,但如果select语句很长的话……有没有更好的方法?
      

  2.   

    declare condition varchar(100)
    ..
     if parm2 is not null 
        condition = and tab.col2=parm2;
     else condition = '';
    ...
    select * from tab
      where tab.col1=parm1||'  '||condition
    嘿嘿,不晓得可不可以。。
      

  3.   

    create or replace procedure mypro(parm1 in number,parm2 in varchar2)
    is 
    begin
      if lenght(parm2)<>0 then
        select * from tab
        where tab.col1=parm1  and tab.col2=parm2;
      else  
        select * from tab
        where tab.col1=parm1  and tab.col2=parm2;  
      end if;
    end;
      

  4.   

    大意写错了:
        else  
        select * from tab
        where tab.col1=parm1  and tab.col2=parm2; 改为:   else  
        select * from tab
        where tab.col1=parm1 ;
      

  5.   

    select * from t 
      where (p_a is null or t.a = p_a)
        and (p_b is null or t.b = p_b)