5

I have 4 tables say, table1, table2, table3 and table4, which are interrelated. Table1 will generate a primary key, that will be used in rest of the tables as reference key.

I have to insert multiple records in table 4 using this primary key. Since the requirement is the transaction should either commit successfully or it should rollback all the changes. That is the reason I thought of writing this in stored procedure. But got stuck, when I had to pass multiple rows data for table4.

Can anyone please suggest, how can I achieve this?

Thanks, in advance.

2
  • is your primarykey autoincremented? Commented Oct 3, 2013 at 11:25
  • Yes, for all the tables, primary key will be auto incremented. Commented Oct 3, 2013 at 11:28

1 Answer 1

2

i guess you want to do something like this

CREATE OR REPLACE PROCEDURE myproc
(
 invId IN NUMBER,
 cusId IN NUMBER
)
IS
    temp_id  NUMBER; 
BEGIN 
    INSERT INTO myTable (INV_ID) 
    VALUES (invId)
    returning id into temp_id;

    INSERT INTO anotherTable (ID, custID) 
    VALUES (temp_id, custId);  
END myproc;
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, right. This will work for first 3 tables, where I have to insert single records. But for 4th table I have to insert multiple records. I don't know how can I achieve that.
i guess you already have the multiple values in parameters of stored procedure. you just have to use temp_id valriable. i will not lose values till stored procedure ends.
No. of parameters of stored procedure will be equal to the no. of columns of all the tables. But in case of 4th table where I have to insert multiple rows, the no. of values can not be fixed.
if you please post the current stored procudure you are using for inserting in 4th table
I have not written any procedure to insert data in 4th table yet.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.