I'm fairly new to Oracle PL/SQL and Im trying to execute a stored procedure which inserts into the orders table and loops through to insert a collection of items into the order_item table. I've created a type which has been defined, but when I try to execute my procedure I get this 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 => '[email protected]', 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_itemsdoesn't make sense;FOR I IN 1..order_itemsmight, if order_items was a number; but it is being treated later as a collection so you probably wantFOR I IN order_items.FIRST..order_items.LAST. Except order_items doesn't seem to be defined. Where is that collection supposed to come from? - Alex PooleFOR I IN 1..order_itemsI getinvalid use of type name or subtype namebut order_items has been defined as a type unless it can't find it. UsingFOR I IN order_items.FIRST..order_items.LASTsaycomponent 'FIRST' must be declared- Jalinsert_orderprocedure which populates data into theorderstable, then executing another procedureinsert_order_items, however an order may have more than one order_item. So I’m trying create a procedure that creates an order calledinsert_orderand another procedure that is called in a loop to insert theorder_itemrows. - Jal