Return Message of Error code in Oracle Stored Proc
Asked Answered
V

2

5

The below procedure (in Oracle 11g release 1) accepts a sql as a parameter & returns its return code.

Returns 0 if success

Returns 1 if no update or no delete performs

Returns actual error code in case of failure.

How can I change below procedure to return me another out param say "return_message" which will contain short description of oracle internal error message? In case of success, it should say "success" and in case no delete/updates performed, it should say "nochange"

CREATE OR REPLACE PROCEDURE "demo"."run_demo"(v_sql IN VARCHAR2, return_code OUT number)
AS

i number;

BEGIN
   return_code := 0;
   execute immediate v_sql;
   i := sql%rowcount;

   IF (i<1)
     THEN return_code := 1;
   END IF;

EXCEPTION  
   WHEN OTHERS THEN
   return_code := SQLCODE;
END;
Valentinavalentine answered 22/12, 2011 at 1:48 Comment(0)
A
9

You want to use the SQLERRM function.

Here's your code:

CREATE OR REPLACE PROCEDURE "demo"."run_demo"(v_sql IN VARCHAR2, return_code OUT number, return_message out varchar2)
AS

i number;

BEGIN
   return_code := 0;
   execute immediate v_sql;
   i := sql%rowcount;

   IF (i<1)
     THEN return_code := 1;
   END IF;

EXCEPTION  
   WHEN OTHERS THEN
   return_message := SQLERRM;
   return_code := SQLCODE;
END;
Alyworth answered 22/12, 2011 at 2:27 Comment(0)
S
3

You can try using SQLERRM to return the message. E.g.,

create or replace procedure RUN_DEMO(V_SQL in varchar2, RETURN_CODE out number, RETURN_MSG out varchar2) as
  I                                number;
begin
  RETURN_CODE := 0;

  execute immediate V_SQL;

  I := sql%rowcount;

  if (I < 1) then
    RETURN_CODE := 1;
  end if;
exception
  when others then
    RETURN_CODE := sqlcode;
    RETURN_MSG := sqlerrm;
end;
Scagliola answered 22/12, 2011 at 2:31 Comment(1)
Ignore this, Daniel beat me to it by a few minutes.Scagliola

© 2022 - 2024 — McMap. All rights reserved.