0% found this document useful (0 votes)
126 views

PLSQL 7 3 Practice

The document discusses trapping user-defined exceptions in PL/SQL code. It provides examples of PL/SQL blocks that update or delete data from a table and include exception handling for conditions like no rows being updated or deleted. The examples demonstrate using RAISE, RAISE_APPLICATION_ERROR and defining custom exceptions.

Uploaded by

Purv Sutariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

PLSQL 7 3 Practice

The document discusses trapping user-defined exceptions in PL/SQL code. It provides examples of PL/SQL blocks that update or delete data from a table and include exception handling for conditions like no rows being updated or deleted. The examples demonstrate using RAISE, RAISE_APPLICATION_ERROR and defining custom exceptions.

Uploaded by

Purv Sutariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

N01580206 Yug Sutariya

academy.oracle.com

Database Programming with PL/SQL


7-3: Trapping User-Defined Exceptions Practice
Activities
Vocabulary

Identify the vocabulary word for each definition below:

A procedure used to return user-defined error messages from stored


Raise Application Error subprograms.

Use this statement to raise a named exception.


Raise
These errors are not automatically raısed by the Oracle Server, but
User-Defined Error are defined by the programmer and are specific to the programmer's
code.

Try It / Solve It

All the questions in this exercise use a copy of the employees table. Create this copy by running the
following SQL statement:

CREATE TABLE excep_emps AS SELECT * FROM employees;


N01580206 Yug Sutariya

1. Create a PL/SQL block that updates the salary of every employee to a new value of 10000 in a
chosen department. Include a user-defined exception handler that handles the condition where no
rows are updated and displays a custom message. Also include an exception handler that will trap
any other possible error condition and display the corresponding SQLCODE and SQLERRM. Test
your code three times, using department_ids 20, 30, and 40.
Ans :-
DECLARE
exce_rows EXCEPTION;
BEGIN
UPDATE excep_emps
SET salary=1000
WHERE department_id=20;
IF SQL%ROWCOUNT=0 THEN
RAISE exce_rows;
END IF;
EXCEPTION
WHEN exce_rows THEN
DBMS_OUTPUT.PUT_LINE('Invalid departmentid');
WHEN OTHERS THEN
N01580206 Yug Sutariya

DBMS_OUTPUT.PUT_LINE(SQLCODE||'-'||SQLERRM);

END;

SELECT * FROM excep_emps;

Department id=20;
N01580206 Yug Sutariya

Department id=30;

Department id=40;
N01580206 Yug Sutariya

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

2. Modify your code from question 1 to handle the condition where no rows are updated using
RAISE_APPLICATION_ERROR procedure in the exception section. Use an error number of –
20202. Test your code again using department_id 40 and check that the
–20202 error is displayed.
Ans :-
DECLARE
exce_rows EXCEPTION;
BEGIN
UPDATE excep_emps
SET salary=1000
WHERE department_id=40;
N01580206 Yug Sutariya

IF SQL%ROWCOUNT=0 THEN
RAISE exce_rows;
END IF;
EXCEPTION
WHEN exce_rows THEN
RAISE_APPLICATION_ERROR(-20202,'Invalid department id');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE||'-'||SQLERRM);

END;
N01580206 Yug Sutariya

3. Modify your code from question 2 to use RAISE_APPLICATION_ERROR in the executable section
instead of the exception section. Test your code again using department_id 40.

Ans :-

DECLARE

exce_rows EXCEPTION;

BEGIN

UPDATE excep_emps

SET salary=1000

WHERE department_id=40;

IF SQL%ROWCOUNT=0 THEN

RAISE_APPLICATION_ERROR(-20202,'Invalid department id');

END IF;

EXCEPTION

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE(SQLCODE||'-'||SQLERRM);

END;
N01580206 Yug Sutariya

4. Be careful incorporating DELETE statements in PL/SQL blocks. If you make a mistake, you may
inadvertently delete data that you didn't mean to delete.
A. Enter and run the following PL/SQL block using department_id = 40, and explain the output.

DECLARE
v_dept_id excep_emps.department_id%TYPE;
v_count NUMBER; BEGIN
v_dept_id := 40;
SELECT COUNT(*) INTO v_count
FROM excep_emps
WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');
DELETE FROM excep_emps
WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees were deleted');
ROLLBACK;
END;
N01580206 Yug Sutariya

B. Modify your code to include two user-defined exception handlers, one to test whether SELECT
returns a value of 0, and the other to test if no rows were DELETEd. Declare the exceptions
and RAISE them explicitly before trapping them in the EXCEPTION section. Do NOT use
RAISE_APPLICATION_ERROR. Test your modified block using department_id 40.

Ans :-
DECLARE
v_dept_id excep_emps.department_id%TYPE;
v_count NUMBER;
exce_exce1 EXCEPTION;
exce_exce2 EXCEPTION;
BEGIN
v_dept_id := 40;
SELECT COUNT(*) INTO v_count
FROM excep_emps
WHERE department_id = v_dept_id;
IF v_count=0 THEN
RAISE exce_exce1;
END IF;
DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');
DELETE FROM excep_emps
WHERE department_id = v_dept_id;
IF SQL%NOTFOUND THEN
RAISE exce_exce2;
END IF;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees were deleted');
ROLLBACK;
EXCEPTION
N01580206 Yug Sutariya

WHEN exce_exce1 THEN


DBMS_OUTPUT.PUT_LINE('Invalid department id');
WHEN exce_exce2 THEN
DBMS_OUTPUT.PUT_LINE('No rows deleted');
END;
N01580206 Yug Sutariya

C. Modify your block again to use RAISE_APPLICATION_ERROR in the executable section. Use
error numbers –20203 and –20204. Test your modified block using department_id 40.

Ans :-
DECLARE
v_dept_id excep_emps.department_id%TYPE;
v_count NUMBER;
exce_exce1 EXCEPTION;
exce_exce2 EXCEPTION;
BEGIN
v_dept_id := 40;
SELECT COUNT(*) INTO v_count
FROM excep_emps
WHERE department_id = v_dept_id;
IF v_count=0 THEN
RAISE_APPLICATION_ERROR(-20203,'Invalid department id');
END IF;
DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');
DELETE FROM excep_emps
WHERE department_id = v_dept_id;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20204,'No rows deleted');
END IF;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees were deleted');
ROLLBACK;
END;
N01580206 Yug Sutariya

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

You might also like