【问题标题】:How to insert multiple items using an insert query in Oracle PL/SQL stored procedure?如何在 Oracle PL/SQL 存储过程中使用插入查询插入多个项目?
【发布时间】: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 插入多个商品

标签: oracle plsql oracle11g


【解决方案1】:

您可以使用函数添加订单详细信息,该函数在成功插入后返回 order_id。 然后使用该订单 ID 使用过程插入到 order_items 表中。

CREATE OR REPLACE FUNCTION INSERT_ORDER RETURN VARCHAR2 IS
    --order specific attributes
    
    BEGIN
    
    --insert into order
    --RETURN order_id;
    
 END INSERT_ORDER;


 CREATE OR REPLACE PROCEDURE INSERT_ORDER_ITEMS(
    --order items specific attributes
    
 BEGIN
    
    --insert into order items
    --COMMIT;
    
 END INSERT_ORDER_ITEMS;
    
 --You can call this within single method/procedure as:
    
 DECLARE
    
 order_id_ NUMBER;
    
 BEGIN
    
    order_id_ := INSERT_ORDER(--attrbiutes);
    FOR i IN i..order_items_list
    LOOP
      INSERT_ORDER_ITEMS(order_id_, --rest of order details);
      COMMIT;
    END LOOP;
 END;

【讨论】:

  • 在尝试实现此方法时遇到一些问题,尤其是当我定义属性时,例如 p_name IN CHAR 我收到此错误 Encountered the symbol "IN" when expecting one of the following: constant exception <an identifier><a double-quoted delimited-identifier> table long double ref char time timestamp interval date binary national character nchar
  • 可能是由于许多问题。确保你也定义了长度:)
  • 好的,谢谢,我对 FOR i IN i..order_items_list 有点困惑,我是不是要在某个地方定义 order_items_list,我对 Oracle PL/SQL 还是很陌生。
  • stackoverflow.com/questions/35231757/… 检查 cmets 定义类型。
  • 我看到使用嵌套关系设计?
猜你喜欢
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 2018-05-17
  • 2018-06-26
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 2017-10-18
相关资源
最近更新 更多