【问题标题】:Oracle Invalid Number error while passing comma separated numbersOracle Invalid Number 错误,同时传递逗号分隔的数字
【发布时间】:2012-12-28 06:46:46
【问题描述】:

这是他的问题描述。 我正在尝试将逗号分隔的整数值(存储在 Varchar 变量中)传递给 IN 条件。

local_var VARCHAR2(20);
local_var := 1,2;
WHERE someid IN (local_var) --Just pasted the where clause where IN condition is being used

someid 的类型为 NUMBER,

我在游标中执行此语句时收到 invalid number 错误。我假设这个问题是因为someid 是一个数字, 可能的解决方案是什么?我不能将 1,2 的值直接传递给 IN 子句,因为我需要根据某些条件构建相同的值,

我也尝试过在引号中传递逗号分隔值,但似乎没有用 使用 Oracle 11.2 版

我尝试过使用

 local_var := '1,2';
 local_var := '''1','2'''; etc..

似乎没有任何效果

需要紧急帮助!提前致谢

【问题讨论】:

    标签: oracle oracle11g data-conversion


    【解决方案1】:

    显然,您会收到该错误,因为someid 是数字数据类型,并且您将逗号分隔的字符串传递给IN 子句。但是您可以使用regexp_substrregexp_count 正则表达式函数将该字符串转换为表行,然后在IN 子句中使用它们。这是一个例子:

    SQL> declare
      2    cursor your_cursor(c_str_value varchar2) is
      3      with str_values as( 
      4        select regexp_substr(c_str_value, '[^,]+', 1, level) as value
      5          from dual
      6        connect by level <= regexp_count(c_str_value, '[^,]+')
      7      )
      8     select first_name   -- here goes your query
      9       from employees e
     10      where e.department_id in (select to_number(value)
     11                                  from str_values);
     12  
     13    l_local_var varchar2(20);
     14  begin
     15    l_local_var := '100,102,103';     -- your comma separated string
     16    for i in your_cursor(l_local_var) -- for the sake of demonstration
     17    loop
     18      dbms_output.put_line(i.first_name);
     19    end loop;
     20  end;
     21  /
    
    Nancy
    Daniel
    John
    Ismael
    Jose Manuel
    Luis
    
    PL/SQL procedure successfully completed
    

    【讨论】:

      【解决方案2】:

      您可以将列表作为参数传递。

      您可以使用动态 PL/SQL 块。比如:

      DECLARE
          local_var VARCHAR2(20) := '1,2';
          req VARCHAR(1000);
      BEGIN
          req := 'BEGIN
              FOR c IN (SELECT someid FROM my_table WHERE someid IN (' || local_var || '))
              LOOP
                  DBMS_OUTPUT.PUT_LINE('someid ' || c.someid);
              END LOOP;
          END;';
          EXECUTE IMMEDIATE req;
      END;
      

      但是……非常小心 PL/SQL 注入。

      【讨论】:

        【解决方案3】:

        你的变量类型是 VARCHAR2 并且你正在分配数字类型,你得到了那个错误。请使用单引号。

        例如#

        local_var VARCHAR2(20);
        local_var := '1';
        WHERE someid IN (local_var);
        

        【讨论】:

        • 如何使用上述实现构建逗号分隔值?
        【解决方案4】:

        就是这样

        create table a(local_var varchar2(10))
        
        insert into a values('2');
        
        select * from a;
        
        declare
        y varchar2(10);
        cursor c1(v_val varchar2) is select * from a;
        Begin
        y := '1,2';
        for i in c1(y)
        loop   
        dbms_output.put_line(i.local_var);
        end loop ;
        end;
        /
        

        【讨论】:

          猜你喜欢
          • 2018-04-23
          • 1970-01-01
          • 2014-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-13
          • 2021-11-28
          相关资源
          最近更新 更多