Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL Exceptions on Update/Delete of non-existing row

Tags:

oracle

plsql

I am learning PL/SQL these days and currently working with Procedures and exceptions using oracle HR schema.

Here is my simple procedure.

create or replace
PROCEDURE DEL_JOB
(p_jobid jobs.job_id%TYPE)
AS 
sqle NUMBER; 
sqlm VARCHAR2(300);
BEGIN
DELETE FROM JOBS 
WHERE JOB_ID = UPPER(p_jobid);
IF SQL%NOTFOUND THEN
  DBMS_OUTPUT.PUT_LINE('No such record');
END IF;
EXCEPTION
 WHEN OTHERS THEN
  sqle := SQLCODE;
  sqlm := SQLERRM;
  DBMS_OUTPUT.PUT_LINE('There is no job with this id that could be deleted');
  DBMS_OUTPUT.PUT_LINE('Error Code ='||sqle||' Error message =' ||sqlm);
END;

When I execute this procedure the output is

  No such record
  PL/SQL procedure successfully complete. 

However, according to the Oracle PDF it should throw an exception and I should really get the message I entered in the exception.

Same thing happened with the Update on non existing record. Please advise. Thanks

like image 830
Adnan Bhatti Avatar asked Jan 12 '12 20:01

Adnan Bhatti


People also ask

What are the 3 types of exceptions in Plsql?

There are three types of exceptions: Predefined exceptions are error conditions that are defined by PL/SQL. Non-predefined exceptions include any standard TimesTen errors. User-defined exceptions are exceptions specific to your application.

How do you handle exceptions in PL SQL block?

To handle raised exceptions, you write separate routines called exception handlers. After an exception handler runs, the current block stops executing and the enclosing block resumes with the next statement. If there is no enclosing block, control returns to the host environment.

Which types of exceptions are implicitly raised?

There are two types of implicit exceptions : Predefined oracle server error : These are most usually occurred oracle errors which are 20 predefined errors. Non Predefined oracle server error: Any other standard oracle errors which are not listed in predefined error are called as non predefined oracle server errors.

What happens if an exception is raised in the program and that exception is not handled by an exception section in either the current or enclosing PL SQL blocks?

If an exception is raised in your program and that exception is not handled by an exception section in either the current or enclosing PL/SQL blocks, that exception is "unhandled." PL/SQL returns the error which raised an unhandled exception all the way back to the application environment from which PL/SQL was run.


2 Answers

I believe SQL%NOTFOUND returns true when no records are found. Your IF would evaluate to true in that case, and therefore write your put_line to terminal. The SQL statement executed successfully. If you execute that SQL statement by itself from command line, you will receive 0 rows updated/deleted, not an Oracle error.

If you want to have an exception thrown, you could use RAISE inside your IF and point it to the exception in the exception block you want to have thrown.

like image 152
Dan Avatar answered Sep 18 '22 08:09

Dan


There is no "exception" - the sql executed successfully. It successfully deleted every record that matched the criteria...which was 0 records. Same thing if a similar update statement was executed. You used the SQL%NOTFOUND to determine there were no records that were affected, but this does not mean there was an "exception".

Perhaps you're thinking of the NO_DATA_FOUND Exception raised if you try a "select into" clause and it doesn't find any matching records.

like image 39
Gerrat Avatar answered Sep 21 '22 08:09

Gerrat