select a.id,b.no from a,b where a.id=b.id and a.id='1' and b.no='2'
union all
select a.id,b.no from a,b where a.id=b.id and a.id='a' and b.no='b'
union all
select a.id,b.no from a,b where a.id=b.id and a.id='y' and b.no='z' 
union all...
目的是写一个procedure,根据输入的参数个数(也就是a.id,b.no),给出查询结果.
不能用in,因为a.id和b.no是成对出现.请高手指点

解决方案 »

  1.   


    用循环分离参数再拼接sql语句,最后用execute immediate 执行
      

  2.   


    有那么麻烦吗?直接用in好,效率也不会太低用union all扫描表的次数多
      

  3.   

    select a.id,b.no from a,b where a.id=b.id and a.id || ',' || b.no in ('1,2','a,b','y,z',...)
      

  4.   

    要在procedure中实现,输入的是两组参数,返回结果.新手,请高手指点.
      

  5.   

    to shiyiwan:
     procedure 中call一张表.
      

  6.   

    一个参数就够了,参数在外面拼好传进来,就像('1,2','a,b','y,z',...)
    里面拼sql,然后execute immediate...
      

  7.   

    那简单了,直接用cursor好了,何必传参呢。
      

  8.   

    procedure 如果不call table,输入的参数可以是数组吗? 
    waterfirer的回答我会试试.
      

  9.   

    pl sql中没有数组类型,如果你想输入这种数据的话,需要先声明一个表类型
    TYPE id_array IS TABLE OF varchar2(20);所以这样还麻烦了,不如在procedure里面直接查表用cursor
      

  10.   

    create or replace procedure proc 
    is 
       v_input  varchar2(2000);   := null;
       
    begin 
       for i in (select id||','||no rec from calltable) 
       loop
          v_input := v_input + ' '''||i.rec||' '',';
       end loop;
       
       v_input := substr(v_input,0,length(v_input)-1);
       
       execute immediate 'select a.id,b.no from a,b where a.id=b.id and a.id,b.no in ('||v_input||')';
    exception
       when others then
          dbms_output.put_line(dbms_utility.format_error_backtrace);
    end proc;
      

  11.   

    oracle 支持对数组字符串的处理,即可以把数组字符串变成二列的结果集存于临时表TT中,如列别名分X,Y 
    然后用以下语句可以实现
    SELECT * FROM  a,b WHERE a.id=b.id AND (A.ID ,B.NO) = (SELECT X,Y FROM TT) 
      

  12.   

    to shiyiwan :
    1) for i in (select id||','||no rec from calltable) 
    for循环不允许那样依次处理表的记录。
    2)查询的结果是一个table,procedure怎样返回?
    execute immediate 'select a.id,b.no from a,b where a.id=b.id and a.id,b.no in ('||v_input||')';
      

  13.   

    多行多列的 就用MAP对象传参吧诸如:
    TYPE FEE_POLICY_MAP IS TABLE OF FEE_POLICY%ROWTYPE INDEX BY BINARY_INTEGER;这样的
      

  14.   

    建立一张临时表tmp(包含两个字段即可:id,no),用来保存查询条件,将所有条件值id与no插入到临时表tmp中对应的id与no字段落,然后通过以下语句实现:
    select a.id,b.no from a,b,tmp c where a.id=b.id and a.id=c.id and b.no=c.no。
    楼主可以尝试此方案
      

  15.   

    where 
    (a.id,b.no) in ('y','z')
      

  16.   

    多个的话
    (a.id,b.no) in (('y','z'),('y','w'))
      

  17.   

    使用15楼的方法或者在procedure里面直接建一个结果临时表execute immediate 'create table tmp as select a.id,b.no from a,b where a.id=b.id and a.id,b.no in ('||v_input||')';