Question

Here is he problem desc. I am trying to pass comma separated integer values(stored in a Varchar variable) to a IN conditon.

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 is of type NUMBER,

I get an invalid number error while executing this statement in a cursor. I am assuming this problem is because of someid being a NUMBER, What is the possible solution? I cannot pass the values say 1,2 directly to IN clause, because I need to build the same based on some conditions,

I have tried passing comma separated values in quotes too, didnt seem to work Using Oracle version 11.2

I have tried using

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

Nothing seemed to work

Urgent help required! Thanks in advance

Was it helpful?

Solution

You are getting that error, obviously, because someid is of number datatype and you are passing a comma separated string, which is considered as one value of string datatype and not a list of values as you would expect, to the IN clause. But you can transform that string, using regexp_substr and regexp_count regular expression functions, into table rows an then use them in the IN clause. Here is an example:

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

OTHER TIPS

You can not pass a list as arguments like that.

You could go with using a Dynamic PL/SQL block. Something like:

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;

But… Be very careful of PL/SQL injections.

your variable type is VARCHAR2 and you are assigning number types, t'y u r getting that error. please use single quotations.

for example #

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

this is the way

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;
/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top