0

I have the following stored procedure which we use to insert data into a table:

CREATE OR REPLACE PROCEDURE mySproc
(
 invoiceId IN NUMBER
 customerId IN NUMBER
)
IS
BEGIN 
    INSERT INTO myTable (INVOICE_ID) 
    VALUES (invoiceId);
END mySproc;
/

What I am trying to do is to get the last inserted ID (this is the primary key field on myTable and auto incremented using a sequence) and insert it into another table, I have tried the following but could not get it working:

CREATE OR REPLACE PROCEDURE mySproc
(
 invoiceId IN NUMBER
 customerId IN NUMBER
)
IS
BEGIN 
    INSERT INTO myTable (INVOICE_ID) 
    VALUES (invoiceId)

    returning id into v_id;

    INSERT INTO anotherTable (ID, customerID) 
    VALUES (v_id, customerId);  
END mySproc;
/

I am getting this error: [Error] PLS-00049 (59: 26): PLS-00049: bad bind variable 'V_ID' I think I need to declare v_id somewhere but I tried before and after the BEGIN statement but that gave another error.

Any ideas as to how to do this?

Thanks

4

1 回答 1

5

将您的程序更改为

CREATE OR REPLACE PROCEDURE mySproc
(
 invoiceId IN NUMBER,  -- Added comma
 customerId IN NUMBER
)
IS
    v_id  NUMBER;  -- ADDED
BEGIN 
    INSERT INTO myTable (INVOICE_ID) 
    VALUES (invoiceId)
    returning id into v_id;

    INSERT INTO anotherTable (ID, customerID) 
    VALUES (v_id, customerId);  
END mySproc;

分享和享受。

于 2013-07-31T11:11:03.507 回答