如何在包含两个插入语句的oracle中执行存储过程?

How to execute stored procedure in oracle that contains two insert statements?

我正在尝试执行包含两个插入语句的存储过程,但我不确定如何执行此操作或是否有更好的方法执行此操作。存储过程有两个插入语句,将数据插入到两个不同的表中。

我已尝试 execute new_order() 请参阅下文然后传入值,但出现此错误 ERROR at line 1: ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'NEW_ORDER' ORA-06550: line 1, column 7: PL/SQL: Statement ignored 非常感谢您的帮助。

 execute new_order(
'4', 
'O223PS562', 
'Test Test', 
'test@test.co.uk', 
'123 Test Street', 
'Newcastle Upon Tyne', 
'Tyne and Wear', 
'NE98 4TN', 
'123456789', 
'7.97', 
'11-apr-2021',
'5', 
'4', 
'2', 
'2', 
'3073748221',
'2', 
'Brand new',
'1.99', 
'2.00');
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
    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
        );

    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;
/

如错误所述,您传递了错误的参数数量或类型。

如果您尝试调用具有这么多参数的过程,我强烈建议您使用命名参数语法。否则,当人们试图弄清楚他们将第 20 个参数传递给过程的内容时,他们往往很难注意到他们遗漏了一个参数或以错误的顺序传递了参数。就个人而言,我倾向于在尝试传递 20 个参数之前很久就重构一个过程。像传递一种或两种记录类型这样简单的事情可能会使代码更易于阅读和理解。

我已将您的调用更改为使用命名参数,并在您出错的地方添加了注释。

execute new_order(
  p_order_id => 4, -- Pass in a number rather than using implicit conversion
  p_order_num => 'O223PS562', 
  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, -- Pass in a number rather than using implicit conversion
  p_order_date => to_date('11-apr-2021', 'DD-mon-YYYY'), -- Pass in a date rather than using implicit conversion
  p_order_item_id => 5, -- Pass in a number rather than using implicit conversion
  p_product_id => 4, -- Pass in a number rather than using implicit conversion
  p_seller_id => 2, -- Pass in a number rather than using implicit conversion 
  p_sub_order_number => 2, -- Pass in a number rather than using implicit conversion 
  p_quantity => 3073748221, -- Do you really want the quantity to be in the billions?  That seems unlikely.  
                            -- Perhaps there was supposed to be a phone number parameter
  p_condition => '2', -- That doesn't look like a condition.  Probably meant to be a unit price
  p_unit_price => 'Brand new', -- Here we have a definite error.  p_unit_price is a number but you can't connvert
                               -- the string 'Brand new' to a number.  I assume that was really supposed to be the
                               -- condition
  p_cost_charge => 1.99, -- Pass in a number rather than using implicit conversion
  '2.00' -- And here we have a value being passed in but no equivalent parameter
);