【发布时间】:2021-07-10 12:10:02
【问题描述】:
我创建了一个存储过程,它有两个插入查询,一个将数据插入到订单表中,另一个将数据插入到 order_item 表中。我希望 order_item 插入查询插入多个项目,我设法让它插入一个项目,但不确定如何将多个项目插入 order_item 表以及如何执行该过程。
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;
/
【问题讨论】:
-
我们不是在您之前的问题中详细介绍了如何调用这个过程吗? stackoverflow.com/questions/67044667/… 通常有一个创建
order的过程和另一个在循环中调用以插入order_item行的过程更有意义。如果要创建多个order_item行,则可以为所有order_item参数传入一堆集合而不是标量值。或者对象的集合。 -
@JustinCave 有效,但只有当订单有一件商品时,我才会尝试让它工作,以便该程序可以根据 order_id 插入多个商品