Sqlplus-在&Quot;子句中使用绑定变量 [英] sqlplus - using a bind variable in "IN" clause

查看:0
本文介绍了Sqlplus-在&Quot;子句中使用绑定变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个PL/SQL块中设置一个绑定变量,并尝试在另一个查询的IN表达式中使用它。大概是这样的:

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);

我在尝试使用"IN"列表中的绑定变量的查询上收到错误(ORA-01722:无效数字)。

print语句产生'123','345',这正是我所期望的。

是否可以像这样使用绑定变量,或者我是否应该尝试其他方法?

(使用Oracle 10g)


澄清:

这是一种和解的事情。我想跑步

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

在脚本的主要部分(这里没有图示)之前删除了一大堆记录。之后我想再次运行它,以验证some_table中的记录未被删除。但是,other_table中的数据确实会被此过程删除,因此我不能只引用other_table中的数据,因为那里什么都没有。我需要一种方法来保留other_table.id值,以便以后可以验证父记录。

推荐答案

我会将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;

这篇关于Sqlplus-在&Quot;子句中使用绑定变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆