Skip to content
Advertisement

How to call a procedure with inserts and an out parameter?

I am working on a procedure to automate some inserts based on an IN parameter. I also want to know if the inserts fail or not, so I created an exception that returns me a ‘FAIL’ if any error occurs. I have the procedure completed, and I’m sure it will do the inserts if I just run the procedure, but I need to use this inside of another one, therefore I need to be able to access the status of the inserts. Usually I would select * into variable, but because I have inserts in my procedure I get errors about using inserts inside of a query. Please help me find a way to be able to call my procedure to know if they fail and act accordingly.

Here is a sample set of data and a sample procedure with similar functionality to mine:

The above proc would then need to be called to 1. do the inserts and 2. tell me if they failed. I tried (without success):

Advertisement

Answer

You don’t need to declare a status variable local to the procedure; its already declared as an OUT parameter. You also should leave the COMMIT and ROLLBACK statements to the block where you call the procedure; this allows multiple procedures to be called in one transaction and then can commit them all or roll them all back. You also don’t use RETURN statements in a procedure.

Then you can call it using PL/SQL (you can’t use procedures in a query, you’d need a function for that):

Which outputs good and puts values into the table:

VAL1 | VAL2 | VAL3
---: | ---: | ---:
   1 |    2 |    3
   1 |    2 |    3
   1 |    2 |    3

db<>fiddle here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement