我想在 Oracle 10g 中执行以下操作(这是一个人为的示例来展示概念,而不是真正的代码)
create table orders (order_id NUMBER);
insert into table orders values (1);
insert into table orders values (2);
insert into table orders values (3);
TYPE NUMBER_ARRAY_T is TABLE of NUMBER;
PROCEDURE VALIDATE_ORDER_IDS(i_orders IN NUMBER_ARRAY_T, o_output OUT SYS_REFCURSOR)
IS
BEGIN
OPEN o_output FOR
select ??? from TABLE(i_orders) where ??? NOT IN (select order_id from orders);
END VALIDATE_ORDER_IDS;
将使用包含 (1,2) 的数组调用存储过程,我们希望得到 3 作为结果
所以,问题是,无论如何要指定一个列名 ???当使用嵌套表作为表时,上面的 select 语句会起作用吗?
最佳答案
您要查找的关键字是 column_value
。根据您的设置:
SQL> CREATE OR REPLACE TYPE NUMBER_ARRAY_T is TABLE of NUMBER;
2 /
Type created
SQL> CREATE OR REPLACE PROCEDURE validate_order_ids(i_orders IN number_array_t,
2 o_output OUT SYS_REFCURSOR)
3 IS BEGIN
4 OPEN o_output FOR
5 SELECT COLUMN_VALUE
6 FROM TABLE(i_orders)
7 WHERE COLUMN_VALUE NOT IN (SELECT order_id FROM orders);
8 END validate_order_ids;
9 /
Procedure created
要调用该过程,您只需:
SQL> VARIABLE x REFCURSOR
SQL> exec validate_order_ids(number_array_t(1,5), :x);
ProcÚdure PL/SQL terminÚe avec succÞs.
SQL> print x
COLUMN_VALUE
------------
5
关于Oracle 嵌套表和列名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1861789/