【发布时间】:2021-07-11 13:17:00
【问题描述】:
我对 Oracle PL/SQL 相当陌生,我试图执行一个存储过程,该过程插入到 orders 表中并循环以将一组项目插入到 order_item 表中。我创建了一个已定义的类型,但是当我尝试执行我的过程时,我得到了这个loop index variable 'I' use is invalid。
SQL> -- Execute procedure
SQL> DECLARE
2 order_id_ NUMBER;
3
4 BEGIN
5 insert_order(p_order_id => 4, p_order_num => 'O223PS56', p_name => 'Test Test', p_email => 'test@test.co.uk', p_address => '123 Test Street', p_city => 'Newcastle Upon Tyne', p_province => 'Tyne and Wear', p_postcode => 'NE98 4TN', p_telephone => '123456789', p_total => 7.97, p_order_date => to_date('11-apr-2021', 'DD-mon-YYYY'));
6 FOR i IN i..order_items
7 LOOP
8 insert_order_items(order_id_, order_items(i) => 5, order_items(i) => 2, order_items(i) => 2, order_items(i) => 3073748221, order_items(i) => 2, order_items(i) => 'Brand New', order_items(i) => 1.99, order_items(i) => 1.99);
9 COMMIT;
10 END LOOP;
11 END;
12 /
FOR i IN i..order_items
*
ERROR at line 6:
ORA-06550: line 6, column 14:
PLS-00364: loop index variable 'I' use is invalid
ORA-06550: line 6, column 5:
PL/SQL: Statement ignored
SQL>
CREATE OR REPLACE PROCEDURE insert_order
(
p_order_id IN INT, p_order_num IN CHAR,
p_name IN CHAR, p_email IN CHAR,
p_address IN VARCHAR2, p_city IN VARCHAR2,
p_province IN VARCHAR2, p_postcode IN VARCHAR2,
p_telephone IN NUMBER, p_total IN NUMBER,
p_order_date IN DATE
)
AS
BEGIN
INSERT INTO orders(order_id, order_number, billing_name, billing_email, billing_address, billing_city, billing_province, billing_postcode, billing_telephone, billing_total, order_date)
values(p_order_id, p_order_num, p_name, p_email, p_address, p_city, p_province, p_postcode, p_telephone, p_total, p_order_date);
COMMIT;
END;
/
CREATE OR REPLACE PROCEDURE insert_order_items
(
p_order_item_id IN INT, p_order_id IN INT,
p_product_id IN INT, p_seller_id IN INT,
p_sub_order_number IN CHAR, p_quantity IN INT,
p_condition IN CHAR, p_unit_price IN NUMBER,
p_cost_charge IN NUMBER
)
AS
BEGIN
INSERT INTO order_item(order_item_id, order_id, product_id, seller_id, sub_order_number, quantity, condition, unit_price, cost_charge)
values(p_order_item_id, p_order_id, p_product_id, p_seller_id, p_sub_order_number, p_quantity, p_condition, p_unit_price, p_cost_charge);
COMMIT;
END;
/
CREATE OR REPLACE TYPE order_items_collection as object(
order_id int, product_id int,
seller_id int, sub_order_number char(10),
quantity int, condition char(100),
unit_price number, cost_charge number
);
/
CREATE OR REPLACE TYPE order_items is table of order_items_collection;
/
【问题讨论】:
-
FOR i IN i..order_items没有意义;FOR I IN 1..order_items可能,如果 order_items 是一个数字;但它稍后会被视为一个集合,因此您可能想要FOR I IN order_items.FIRST..order_items.LAST。除了 order_items 似乎没有定义。该集合应该来自哪里? -
@AlexPoole 当更改为
FOR I IN 1..order_items我得到invalid use of type name or subtype name但 order_items 已被定义为一种类型,除非它找不到它。使用FOR I IN order_items.FIRST..order_items.LAST说component 'FIRST' must be declared -
但是您需要一个该类型的 instance - 即该类型的变量,您已使用要在循环中引用的数据填充该变量。但是,您在循环中所做的事情没有意义。为什么你有那个类型和集合——程序不使用它?目前还不清楚您要做什么。
-
所以我要做的是执行
insert_order过程,它将数据填充到orders表中,然后执行另一个过程insert_order_items,但是一个订单可能有多个order_item .因此,我正在尝试创建一个过程,该过程创建一个名为insert_order的订单,以及另一个在循环中调用以插入order_item行的过程。 -
您有订单和单个商品的硬编码值。也许你想传入一个项目集合,也许是订单本身的一个对象;但这不是你目前正在做的事情。