Sqlplus - using the bind variable in the "IN" clause

I am setting a bind variable in a PL / SQL block, and I am trying to use it in another query IN expression. Something like that:

variable x varchar2(255)

declare
    x varchar2(100);
begin
    for r in (select id from other_table where abc in ('&val1','&val2','&val3') ) loop
    x := x||''''||r.id||''',';
    end loop;
    --get rid of the trailing ','
    x:= substr(x,1,length(x)-1);

    select x into :bind_var from dual;
end;
/

print :bind_var;

select *
from some_table
where id in (:bind_var);

And I get an error message (ORA-01722: Invalid number) in a request that is trying to use the bind variable in the "IN" list.

The yiels print statement '123','345', which I expect.

Is it possible to use such a binding variable, or should I try a different approach?

(using Oracle 10g)


Clarification:

This is for reconciliation. I want to run

select *
from some_table
where id in (select id from other_table where abc in ('&val1','&val2','&val3'))

, script ( ) . , , some_table . other_table , other_table, . other_table.id, .

+3
5

other_table.id PL/SQL :

type t_id_table is table OF other_table.id%type index by binary_integer;
v_table t_id_table;

-- fill the table
select id
bulk collect into v_table
from other_table 
where abc in ('&val1','&val2','&val3');     

-- then at a later stage...    

select *
from some_table st
,    table(cast(v_table AS t_id_table)) idt
where st.id = idt.id;
+6

, , .

:

select * from some_table where id in (:bind_var1, :bind_var2)

- :

select * from some_table where id in ("select blah blah blah...");
+1

create global temporary table gtt_ids( id number ) ;

...
for r in (select id from other_table where ... ) loop
   insert into gtt_ids(id) values (r.id) ;
end loop;
...

select *
from some_table
where id in (select id from gtt_ids);
+1

, , ...

col idList NEW_VALUE v_id_list /* This is NEW! */
variable x varchar2(255)
declare
    x varchar2(100);
begin
    for r in (select id from other_table where abc in ('&val1','&val2','&val3') ) loop
    x := x||''''||r.id||''',';
    end loop;
    --get rid of the trailing ','
    x:= substr(x,1,length(x)-1);

    select x into :bind_var from dual;
end;
/

print :bind_var;

select :x idList from dual;  /* This is NEW! */

select *
from some_table
where id in (&idList);  /* This is CHANGED! */

, - , .

0

, listagg ( , 11gr2).

( 10g substr, ), .

    variable bind_var varchar2(255)
variable dataSeperationChar varchar2(255)

declare
    x varchar2(100);
begin

select listagg(id,',')  within group(order by id) idList
into x
  from(select level id 
         from dual  
        connect by level < 100 ) 
where id in (&val1,&val2,&val3) ;
    select x into :bind_var from dual;
    :dataSeperationChar := ',';
end;
/

print :bind_var;

/



select *
  from (
        select level id2
          from dual
         connect by level < 100
        )
    where id2 in(
            select  -- transform the comma seperated string into a result set        
            regexp_substr(:dataSeperationChar||:bind_var||','
                        , '[^'||:dataSeperationChar||']+'
                      ,1
                      ,level)    as parsed_value
            from dual
            connect by level <= length(regexp_replace(:bind_var, '([^'||:dataSeperationChar||'])', '')) + 1    
    )
;

/*
values of 1,5, and 25

BIND_VAR
------
1,5,25

ID2                    
---------------------- 
1                      
5                      
25   
*/


, 10g, , , listagg,

0

Source: https://habr.com/ru/post/1791574/


All Articles