I've created a stored procedure which has two insert queries, one inserts data into the orders table and the other inserts data into the order_item table. I want the order_item insert query to insert mutiple items, I've managed to get it inserting one item but unsure how to insert multiple items into the order_item table and also how to execute the procedure.
CREATE OR REPLACE PROCEDURE new_order
-- define the variables for orders and order_item tables
(
-- variables for the orders table
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,
-- variables for the order_item table
p_order_item_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
DBMS_OUTPUT.PUT_LINE ('Insert attempted');
-- Insert data into the order table
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
);
-- Insert data into the order_item table
-- Loop through the order items based on the order_id
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;
DBMS_OUTPUT.PUT_LINE ('Insert succeeded');
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.PUT_LINE ('Insert rejected');
DBMS_OUTPUT.PUT_LINE ('SQL Error Code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE ('SQL Error Message: ' || SQLERRM);
ROLLBACK;
END;
/