想要执行以下sql语句
select * from testTabel where colName in ('a', 'b')用plsql实现的相关代码
declare
TYPE Tab_Test IS TABLE OF testTable.colName%TYPE INDEX BY BINARY_INTEGER;
tar Tab_Test;
begin 
  tar(1) := 'a';
  tar(2) := 'b';  select * from testTabel where colName in 这里怎么写?
end注:主要是想问table类型怎么用在in的查询条件里,下面这样的方法不行。
var := '(' || tar(1) || ', ' || tar(2) || ')';
就是单引号的问题

解决方案 »

  1.   

    var := '(’' || tar(1) || '‘, ’' || tar(2) || '‘)';
    连续两个单引号也不行
      

  2.   

    三个单引号算一个引号。。
    '(''' ||tar(1)||''','''||tar(2)||''')' 
      

  3.   


    select * from testTabel 
    where colName in '(''' || tar(1) || ''',''' || tar(2) || ''')'
      

  4.   


    Thank you!!可是,有没有什么系统包之类的呢,或是其它的方法 ,用来简化。
    比如说在select中,对于table类型,可以这样用
    select colName BULK COLLECT INTO tar FROM testTabel ;
    就把查寻结果全都装入tar中了
      

  5.   

    可以使用execute immediate v_sql into xxx using .........
      

  6.   


    好像还有问题,东西没有查出来。
    直接在plsql里使用select * from testTabel where colName in ('a', 'b'),是有查寻结果的。
    按照上面方法拼接,相应部分确实变成了('a', 'b')的形式。但是什么都没有查到。
      

  7.   


    SQL> declare
      2    a     number := 1;
      3    b     number := 2;
      4    c     number;
      5  begin
      6    execute immediate 'select 1 from dual where 1 between :a and :b'
      7      into c
      8      using a, b;
      9  end;
     10  /PL/SQL procedure successfully completedSQL> 
      

  8.   


    select * from testTabel where colName in ('a', 'b')

    select * from testTabel where colName in '(''' || tar(1) || ''',''' || tar(2) || ''')'有区别吗,怎么会一个能查到,一个查不到?
    var := '(''' || tar(1) || ''', '''|| tar(2) || ''')';
    DBMS_OUTPUT.PUT_LINE(var);
    查看是没有问题的,为('a', 'b')
      

  9.   

    declare
    TYPE Tab_Test IS TABLE OF testTable.colName%TYPE INDEX BY BINARY_INTEGER;
    tar Tab_Test;
    begin  
      tar(1) := 'a';
      tar(2) := 'b';
      execute immediate 'select * from testTabel where colName(:1,:2)' into xxxx using tar(1),tar(2);
    end
      

  10.   


    我的colName列是varchar2,里面存的是各种字符串
      

  11.   

    好像有点问题:无效的关系运算符
    而且,假设tar有100项,这样会不会不太好写
      

  12.   

    select * from testTabel where colName in ('a', 'b')查询的是a ,b select * from testTabel where colName in ('''a''','''b''') 查询的是'a','b'
      

  13.   

    所以有没有类似查寻时的BULK COLLECT INTO,这样的方法,直接可以从table中取内容,呵呵
      

  14.   

    select * from testTabel where colName in (tar(1),tar(2))
      

  15.   

    execute '' bulk collect into .... using....一样的使用哈 。。
      

  16.   

    全好了,呵呵TYPE TYPE_CIRELATION IS TABLE OF testtable.sourceci%TYPE INDEX BY BINARY_INTEGER;
    rel TYPE_CIRELATION;
    tar TYPE_CIRELATION;
    var varchar2(150);begin
    tar(1):='c';
      tar(2):='a';
      var := '(''' || tar(1) || ''','''|| tar(2) || ''')';
      
      var := 'select sourceci from testtable where targetci in' || var;
      
      execute immediate var  bulk collect into rel;
    end
      

  17.   

    连接两个字符串 可以用CONCAT 也可以用 ||   多个的时候必须用 ||了