--比如有如下代码:begin  v_s1 := 'abc';
  v_s2 := '123';  select count(1) into v_count from t1 where x in (v_s1, v_s2);
 
end;
--v_count的结果是0, 请教如何正确改写这段代码? --另请教v_s1, v_s2可改为 嵌套表 或 Varry类型吗?--多谢!

解决方案 »

  1.   

    v_count结果为0不对么?说明没有列x值为'abc'或者'123'的记录呀,这样写就是对的。
      

  2.   

    非也.
    当改成select count(1) into v_count from t1 where x in ('abc', '123'); 就有结果了, 不信你测试一下试试.
      

  3.   

    这样写应该没什么问题
    declare
    v_s1 varchar2(10);
    v_s2 varchar2(10);
    v_count int;
    begin
    v_s1 := 'abc';
    v_s2 := '123';
    select count(1) into v_count from t1 where x in (v_s1,v_s2);
    dbms_output.put_line(v_count);
    end;
    /
      

  4.   


    非也. 
    当改成select count(1) into v_count from t1 where x in ('abc', '123'); 就有结果了, 不信你测试一下试试.
      

  5.   


    改成select count(1) into v_count from t1 where x in ('abc', '123') 就有结果了, 不信你测试一下试试
      

  6.   

    --v_count的结果是0, 请教如何正确改写这段代码?  是正确的,可以为0,也可以赋值的。
    --另请教v_s1, v_s2可改为 嵌套表 或 Varry类型吗? 不可以的,里面必须是一个一个值,并且用,符号连接起来。
      

  7.   

    09:15:32 scott@TUNGKONG> select * from t1;X
    ----------
    abc已用时间:  00: 00: 00.00
    09:15:39 scott@TUNGKONG> declare
    09:15:50   2  v_s1 varchar2(10);
    09:15:50   3  v_s2 varchar2(10);
    09:15:50   4  v_count int;
    09:15:50   5  begin
    09:15:50   6  v_s1 := 'abc';
    09:15:50   7  v_s2 := '123';
    09:15:50   8  select count(1) into v_count from t1 where x in (v_s1,v_s2);
    09:15:50   9  dbms_output.put_line(v_count);
    09:15:50  10  end;
    09:15:50  11  /
    1PL/SQL 过程已成功完成。
    这不有结果吗?
      

  8.   

    select count(1) into v_count from t1 where x in (v_s1,v_s2);改成execute immediate 'select count(1) from t1 where x in ('||v_s1||','||v_s2||')' into v_count;
      

  9.   


    这是我的结果, 难道是版本或补丁问题?
    SQL> select distinct c3 from t1;
     
    C3
    ----------
    w0
    w1
    w2
    ------------------------- 
    SQL> select count(1) from t1 where c3 in ('w1','w2');
     
      COUNT(1)
    ----------
             6SQL> declare
      2  v_count integer;
      3  v_s1 varchar2(20);
      4  v_s2 varchar2(20);
      5  begin
      6  v_s1 := 'w1';
      7  v_s2 := 'w2';
      8  select count(1) into v_count from t1 where c3 in (v_s1, v_s2);
      9  dbms_output.put_line(v_count);
     10  end;
     11  /
     
    0
     
    PL/SQL procedure successfully completed
     
    SQL>  
      

  10.   

    找到1半原因了:字段是char类型, 不是varchar.改成select count(1) into i from t1 where rtrim(c3) in (v_s1, v_s2)就可以了不过纳闷的是:为何select count(1) into i from t1 where c3 in ('w1', 'w2')就没问题了?以前搞sql server, 发现oracle有很多让我迷惑的地方, 请大家多指教啊
      

  11.   

    09:36:26 scott@TUNGKONG> select count(1) from t1 where c3 in ('w1','w2');  COUNT(1)
    ----------
             2已用时间:  00: 00: 00.03
    09:36:54 scott@TUNGKONG> declare
    09:37:38   2  v_count integer;
    09:37:38   3  v_s1 varchar2(20);
    09:37:38   4  v_s2 varchar2(20);
    09:37:38   5  begin
    09:37:38   6  v_s1 := 'w1';
    09:37:38   7  v_s2 := 'w2';
    09:37:38   8  select count(1) into v_count from t1 where c3 in (v_s1, v_s2);
    09:37:38   9  dbms_output.put_line(v_count);
    09:37:38  10  end;
    09:37:38  11  /
    2PL/SQL 过程已成功完成。已用时间:  00: 00: 00.01
      

  12.   

    该不会表里有名叫v_s1或v_s2的字段吧
      

  13.   

    找到1半原因了:字段是char类型, 不是varchar. 改成select count(1) into i from t1 where rtrim(c3) in (v_s1, v_s2)就可以了 不过纳闷的是:为何select count(1) into i from t1 where c3 in ('w1', 'w2')就没问题了? 以前搞sql server, 发现oracle有很多让我迷惑的地方, 请大家多指教啊
      

  14.   

    09:44:28 scott@TUNGKONG> desc t1;
     名称                                                  是否为空? 类型
     ----------------------------------------------------- -------- ------------------------------------
     C3                                                             CHAR(10)09:44:32 scott@TUNGKONG> select * from t1;C3
    ----------
    w0
    w1
    w2已用时间:  00: 00: 00.00
    09:44:36 scott@TUNGKONG> declare
    09:44:48   2  v_count integer;
    09:44:48   3  v_s1 t1.c3%type;
    09:44:48   4  v_s2 t1.c3%type;
    09:44:48   5  begin
    09:44:48   6  v_s1 := 'w1';
    09:44:48   7  v_s2 := 'w2';
    09:44:48   8  select count(1) into v_count from t1 where c3 in (v_s1, v_s2);
    09:44:48   9  dbms_output.put_line(v_count);
    09:44:48  10  end;
    09:44:48  11  /
    2PL/SQL 过程已成功完成。已用时间:  00: 00: 00.00
    09:44:49 scott@TUNGKONG> declare
    09:44:55   2  v_count integer;
    09:44:55   3  v_s1 varchar2(10);
    09:44:55   4  v_s2 varchar2(10);
    09:44:55   5  begin
    09:44:55   6  v_s1 := 'w1';
    09:44:55   7  v_s2 := 'w2';
    09:44:55   8  select count(1) into v_count from t1 where c3 in (v_s1, v_s2);
    09:44:55   9  dbms_output.put_line(v_count);
    09:44:55  10  end;
    09:44:55  11  /
    0PL/SQL 过程已成功完成。已用时间:  00: 00: 00.00
      

  15.   

    09:46:43 scott@TUNGKONG> declare
    09:46:44   2  v_count integer;
    09:46:44   3  v_s1 char(10);
    09:46:44   4  v_s2 char(10);
    09:46:44   5  begin
    09:46:44   6  v_s1 := 'w1';
    09:46:44   7  v_s2 := 'w2';
    09:46:44   8  select count(1) into v_count from t1 where c3 in (v_s1, v_s2);
    09:46:44   9  dbms_output.put_line(v_count);
    09:46:44  10  end;
    09:46:44  11  /
    2PL/SQL 过程已成功完成。
    =====================================
    主要问题在于你程序中定义的类型和表中字段类型不一致导致的.........
      

  16.   

    原来是这么回事..
    定义变量的时候可以使用
    declare
      v_s1 t1.x%type:= 'abc'; 
      v_s2 t1.x%type:= '123';
    就不会有类型上的问题
      

  17.   

    受教了, 原来oracle还有如此规则, 多谢!