【发布时间】:2012-11-17 15:03:42
【问题描述】:
我想将一个数字列表查询到一个 plsql 变量中,并在另一个 sql 查询的 in 子句中使用它。我在下面创建了一个我想做的测试用例。
我在谷歌上搜索了解决方案,我认为它一定是可能的,但我就是没有让它运行。请帮助我提供编译解决方案。
CREATE OR REPLACE PROCEDURE PROCEDURE1
as
type t_id is table of number;
v_ids t_id;
v_user_ids number;
BEGIN
-- fill variable v_id with id's, user_id is of type number
select user_id
bulk collect into v_ids
from user_users;
-- then at a later stage ... issue a query using v_id in the in clause
select user_id into v_user_ids from user_users
-- this line does not compile ( local collection type not allowed in SQL statements)
where user_id in ( v_ids );
END PROCEDURE1;
【问题讨论】:
-
也许这是一个简化的演示,但如果你给出的情况是你不只是将两个查询合二为一吗? “选择 ... from ... 其中 user_id 在(从 ...中选择 user_id)”?还是执行联接?
-
是的,这是愚蠢的,可能没有意义。我需要编译示例 - 我在行中收到错误: where user_id in ( v_ids )
-
使用 sql 数组与 plsql 嵌套表不是您的选择吗?如果是,我们可以使用 TABLE() 函数来解决问题(尽管您的最后一条语句没有意义,因为它是一个选择,所以您最终只有一行?如果您真正的 sql 是 select max () 或 rownum = 1 什么的)。
-
slq 数组:我声明'type t_id is varray(1000) of number;'并使用'where user_id in (table(v_ids));'但这也不编译
-
@user1863377 我的意思是,
create type t_id as table of number;而不是在你的 PLSQL 块中。这可以接受吗?
标签: oracle plsql oracle10g bind-variables