傳入一個字符串好了。
直接傳入one = key1
 and  two = key2
 and  there = key3
 and  four = key4
 and  five = key5
這樣的條件。
那你的oracle過程裡面就很好處理了。因為前台獲取這個條件是很方便的。

解决方案 »

  1.   

    同意
    在外面拼一个SQL比较方便的说
    不行就这样key1为null就传入%
    函数里用like key
      

  2.   

    动态执行吧
    根据输入参数是否为空组合出sql语句
      

  3.   

    SQL> select * from a;ID   ST C  D
    ---- -- -- ----
    11   2  19 48
    12   2  29 58
    13   2  39 68
    SQL> create or replace procedure p_1(c1 in varchar2,c2 in  varchar2)
      2  as
      3  str varchar2(100);
      4  n number;
      5  begin
      6  str:='select 1 from a where';
      7  if c1 is not null then
      8  str:=str||' c=:c1 and';
      9  end if;
     10  if c2 is not null then
     11  str:=str||' d=:c2 and';
     12  end if;
     13  str:=substr(str,1,length(str)-4);
     14  execute immediate str into n using c1,c2;
     15  dbms_output.put_line(n);
     16  end;
     17  /过程已创建。SQL> exec p_1('19','48');
    1PL/SQL 过程已成功完成。
      

  4.   

    SQL> select * from a;ID   ST C  D
    ---- -- -- ----
    11   2  19 48
    12   2  29 58
    13   2  39 68SQL> create or replace procedure p_1(c1 in varchar2,c2 in  varchar2)
      2  as
      3  str varchar2(100);
      4  n number;
      5  begin
      6  str:='select 1 from a where';
      7  if c1 is not null then
      8  str:=str||' c=:c1 and';
      9  end if;
     10  if c2 is not null then
     11  str:=str||' d=:c2 and';
     12  end if;
     13  str:=substr(str,1,length(str)-4);
     14  execute immediate str into n using c1,c2;
     15  dbms_output.put_line(n);
     16  end;
     17  /过程已创建。SQL> exec p_1('19','48');
    1PL/SQL 过程已成功完成。
      

  5.   

    这样可以实现
     one = nvl(key1,one)
     and  two = nvl(key2,two)
     and  there = nvl(key2,there)
     and  four = nvl(key2,four)
     and  five =nvl(key2,five)
      

  6.   

    create or replace procedure key_pro
    (p_key1 in integer,
     p_key2 in integer,
     p_key3 in integer,
     p_key4 in integer,
     p_key5 in integer)
    is
    begin
    declare 
    v_sql  varchar2(40);
    v_sqlnew varchar2(40)
    vsql:='select * from table '
    if p_key1 is not null then
    vsql:=vsql||' where one=p_key1';
    elseif p_key2 is not null then
    vsql:=vsql||' and two=p_key2';
    elseif p_key3 is not null then
    vsql:=vsql||' and three=v_key3';
    elseif p_key4 is not null then
    vsql:=vsql||'and four=p_key4';
    elseif p_key5 is not null then
    vsql:=vsql||' and five=p_five';
    ebd if
    v_sqlnew:=replace(vsql,''',' ");
    exception
    when sql%found then
    dbms_output.put_line('select error');
    end key_pro;
      

  7.   

    我的写法需要至少有一个参数不是空楼上的写法有bug哦
    如果第一个参数是空的话,组合出的语句就不对了
    :)
      

  8.   

    谢谢各位的回答To: jiezhi() , dukeli(我就是传说中的菜鸟) , bzszp(SongZip) , vrv0129() >>
        感觉上不管在外面拼SQL文传入,还是在函数组合,都需要用动态执行.因为考虑到编码规范,所以想尽量避免使用.是不是这类问题只能用动态执行呢?请高手指教To: LGQDUCKY(飘) >>
        "one = nvl(key1,one)"
        当one本身为null时,如果key1为null的情况下,会出现null=null的情况,好象就没有数据选出来了,是这样吗?
      

  9.   

    你們的編碼規范不允許動態執行sql語句?哈哈,那這樣的規范問題就大了。
    你只是拼接一個sql語句讓oracle執行而已,和執行別的sql有何種區別?
    如果規范中不允許動態sql(這個可是指在存儲過程中拼接sql並且執行),那你完全可以在界面端就拼接好sql。而且如果不允許使用動態sql,那也太不合理了吧
      

  10.   

    a[1]:=one;
    .
    .
    .
    a[5]=five;
    sql_where:=a[1];
    for i in 2..5 loop
      if a[i]!=null then
         a[i]='and'||a[i];
        sql_where:=sql_where||a[i];
      end if;
    end loop;
      

  11.   

    更正:
    a[5]:=five;
    a[i]:='and'||a[i];
      

  12.   

    select * from tab1 
    where nvl2(key1,one,1)=nvl(key1,1)
    and  nvl2(key2,two,1)=nvl(key2,1)
    and  nvl2(key3,there,1)=nvl(key3,1)
    and  nvl2(key4,four,1)=nvl(key4,1)
    and  nvl2(kdy5,five,1)=nvl(key5,1)
      

  13.   

    感谢各位,提供了这么多优秀的,解决问题的方法。
    因为用的是9i,比较偏向beckhambobo(beckham) 提供的方法,能少打点字。
    再次感谢各位的帮助。
    偶结贴了。