In oracle, I want to create a delete sproc that returns an integer based on the outcome of the deletion.
this is what i have so far.
create or replace
PROCEDURE Testing
(
iKey IN VARCHAR2
)
AS
BEGIN
delete from MyTable WHERE
TheKey = iKey;
END Testing;
i've tried putting a RETURNS INTEGER in but the sproc won't compile.
A procedure does not return a value. A function returns a value, but you shouldn't be doing DML in a function (otherwise you cannot do things like reference the function in a SQL statement, you confuse permission grants since normally DBAs want to be able to grant read-only users access to all the functions so that users are doing computations consistently, etc.).
You can add an OUT parameter to the procedure to return the status. If "success" means that one or more rows were updated, you can use SQL%ROWCOUNT to get a count of the number of rows modified by the prior SQL statement and use that to populate the return parameter, i.e.
Of course, from a general code clarity standpoint, I'm dubious about OUT parameters that appear to be trying to return a status code. You are generally much better served by assuming success and throwing exceptions in the event of an error.
You can use a stored procedure to return results.
To call the procedure use something like:
Use a function and the implicit SQL cursor to determine the number of rows deleted
That should work
You are probably looking for a function instead.
Note: Where I work we generally put functions inside a sql package.
The
SQL%ROWCOUNT
will only return a value immediately after a DML its value will reset if another DML is executed.To get around the problem as I was executing deletes in a loop I issued the following command after the dml:
Please make sure you declare and initialize
row_count := 0;