oracle procedure returns integer oracle procedure returns integer sql sql

oracle procedure returns integer


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.

CREATE OR REPLACE PROCEDURE test_proc (  p_iKey    IN VARCHAR2,  p_retVal OUT INTEGER)ASBEGIN  DELETE FROM myTable   WHERE theKey = p_iKey;  IF( SQL%ROWCOUNT >= 1 )  THEN    p_retVal := 1;  ELSE    p_retVal := 0;  END IF;END test_proc;

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.

CREATE OR REPLACE PROCEDURE testing (iKey IN VARCHAR2, oRes OUT NUMBER)ASBEGIN   DELETE FROM MyTable         WHERE TheKey = iKey;   oRes := SQL%ROWCOUNT;END;

To call the procedure use something like:

DECLARE   pRes   NUMBER;BEGIN   testing ('myspecialkey', pRes);   DBMS_OUTPUT.put_line (pRes);END;


Use a function and the implicit SQL cursor to determine the number of rows deleted

create or replaceFUNCTION Testing( iKey IN VARCHAR2) RETURN INTEGER AS BEGIN  delete from MyTable WHERE   TheKey = iKey;  RETURN SQL%ROWCOUNT;END Testing;

That should work