使用 table 作为 IN 参数调用过程
call a procedure with table as IN parameters
我有以下代码,效果很好:
declare
v_order_id oe.orders.order_id%type := 1;
v_order_item pkg_order_management.to_order_list := pkg_order_management.to_order_list();
begin
v_order_item.extend(2);
v_order_item(1).product_id := 2289;
v_order_item(1).quantity := 2;
v_order_item(2).product_id := 2058;
v_order_item(2).quantity := 5;
pkg_order_management.prc_create_order(240, v_order_item, v_order_id);
dbms_output.put_line('it was created the order: ' || v_order_id);
end;
但我想调用 pkg_order_management.prc_create_order 程序,例如
declare
v_order_id oe.orders.order_id%type := 1;
begin
pkg_order_management.prc_create_order(240, ((2289, 2),(2058, 5)), v_order_id);
dbms_output.put_line('it was created the order: ' || v_order_id);
end;
这是包中的类型定义:
type t_order_item is record
( product_id oe.order_items.product_id%type
, quantity oe.order_items.quantity%type);
type to_order_list is table of t_order_item;
当我调用第二种情况下的过程时,我收到以下错误:
PLS-00306: wrong number or types of arguments in call to
'PRC_CREATE_ORDER'
当然,我的调用类型是错误的,但我不知道如何解决这个问题。
你能给我一个提示吗?
正确的调用应该是这样的:
pkg_order_management.prc_create_order(
240,
pkg_order_management.to_order_list(
t_order_item(2289, 2),
t_order_item(2058, 5)
),
v_order_id
);
...但是你真的想要这么难看的代码吗?
pkg_order_management.prc_create_order(240, v_order_item, v_order_id);
我觉得更漂亮了
PL/SQL 记录类型不是面向对象的结构。所以我们不能像实际使用对象那样灵活地使用它们。
如果你想传递一个内联数组,你需要使用 SQL:
来定义你的类型
create or replace type t_order_item is object
( product_id number
, quantity number);
/
create or replace type to_order_list is table of t_order_item;
/
请注意,这意味着您不能再使用 %TYPE 引用来定义 t_order_item
的属性。
现在您对该过程的调用将如下所示:
begin
pkg_order_management.prc_create_order(240
, to_order_list(t_order_item(2289, 2)
, t_order_item(2058, 5)
), v_order_id);
end;
/
您需要使用 OBJECT
而不是 RECORD
。
请红色:
https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/objects.htm
那么你可以使用这个漂亮的符号:
CREATE TYPE t_order_item AS OBJECT (
product_id NUMBER,
quantity NUMBER
);
/
CREATE TYPE to_order_list IS TABLE OF t_order_item;
/
DECLARE
v_order_id NUMBER;
PROCEDURE prc_create_order (a NUMBER, order_list to_order_list, o_order_id OUT NUMBER)
IS
BEGIN
NULL;
END;
BEGIN
prc_create_order(240, to_order_list(
t_order_item(2289, 2),
t_order_item(2058, 5)
)
, v_order_id);
END;
/
我有以下代码,效果很好:
declare
v_order_id oe.orders.order_id%type := 1;
v_order_item pkg_order_management.to_order_list := pkg_order_management.to_order_list();
begin
v_order_item.extend(2);
v_order_item(1).product_id := 2289;
v_order_item(1).quantity := 2;
v_order_item(2).product_id := 2058;
v_order_item(2).quantity := 5;
pkg_order_management.prc_create_order(240, v_order_item, v_order_id);
dbms_output.put_line('it was created the order: ' || v_order_id);
end;
但我想调用 pkg_order_management.prc_create_order 程序,例如
declare
v_order_id oe.orders.order_id%type := 1;
begin
pkg_order_management.prc_create_order(240, ((2289, 2),(2058, 5)), v_order_id);
dbms_output.put_line('it was created the order: ' || v_order_id);
end;
这是包中的类型定义:
type t_order_item is record
( product_id oe.order_items.product_id%type
, quantity oe.order_items.quantity%type);
type to_order_list is table of t_order_item;
当我调用第二种情况下的过程时,我收到以下错误:
PLS-00306: wrong number or types of arguments in call to 'PRC_CREATE_ORDER'
当然,我的调用类型是错误的,但我不知道如何解决这个问题。 你能给我一个提示吗?
正确的调用应该是这样的:
pkg_order_management.prc_create_order(
240,
pkg_order_management.to_order_list(
t_order_item(2289, 2),
t_order_item(2058, 5)
),
v_order_id
);
...但是你真的想要这么难看的代码吗?
pkg_order_management.prc_create_order(240, v_order_item, v_order_id);
我觉得更漂亮了
PL/SQL 记录类型不是面向对象的结构。所以我们不能像实际使用对象那样灵活地使用它们。
如果你想传递一个内联数组,你需要使用 SQL:
来定义你的类型create or replace type t_order_item is object
( product_id number
, quantity number);
/
create or replace type to_order_list is table of t_order_item;
/
请注意,这意味着您不能再使用 %TYPE 引用来定义 t_order_item
的属性。
现在您对该过程的调用将如下所示:
begin
pkg_order_management.prc_create_order(240
, to_order_list(t_order_item(2289, 2)
, t_order_item(2058, 5)
), v_order_id);
end;
/
您需要使用 OBJECT
而不是 RECORD
。
请红色: https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/objects.htm
那么你可以使用这个漂亮的符号:
CREATE TYPE t_order_item AS OBJECT (
product_id NUMBER,
quantity NUMBER
);
/
CREATE TYPE to_order_list IS TABLE OF t_order_item;
/
DECLARE
v_order_id NUMBER;
PROCEDURE prc_create_order (a NUMBER, order_list to_order_list, o_order_id OUT NUMBER)
IS
BEGIN
NULL;
END;
BEGIN
prc_create_order(240, to_order_list(
t_order_item(2289, 2),
t_order_item(2058, 5)
)
, v_order_id);
END;
/