Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Handling exception for Update statement

DevguyOct 11 2015 — edited Oct 12 2015

Hi All,

I was just browsing across the new edition of Steven's book and was reading about exception handling part, I just wondered if we could handle exception for update statement with when others then

BEGIN

UPDATE emp

SET sal=5000

WHERE empno = 8000;

EXCEPTION WHEN OTHERS THEN

dbms_output.put_line('Update Failed-'||sqlerrm);

END;

Output:

How I handle it

DECLARE

   p_count   NUMBER;

BEGIN

   UPDATE emp

      SET sal = 5000

    WHERE empno = 8000;

   p_count := SQL%ROWCOUNT;

   IF p_count = 0

   THEN

      DBMS_OUTPUT.put_line ('Update Failed-' || p_count || ' rows updated');

   END IF;

END;

Output:

Update Failed-0 rows updated

Is this the right approach to handle exception for an update statement?

Please advise!

Thanks!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 9 2015
Added on Oct 11 2015
5 comments
2,793 views