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

PLSQL 7 3 Practice

don't read me
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PLSQL 7 3 Practice

don't read me
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

www.oracle.

com/academy

Database Programming with PL/SQL


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

Identify the vocabulary word for each definition below:


Raise Application Error A procedure used to return user-defined error messages from
stored subprograms.

Raise Use this statement to raise a named exception.

User-Defined Error These errors are not automatically raısed by the Oracle Server,
but 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;

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.

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.
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.

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;

El bloque PL/SQL inicial selecciona el número de empleados en el departamento con


department_id = 40, muestra el conteo, elimina los empleados de ese departamento, muestra
cuántos empleados fueron eliminados y revierte la eliminación. Al ejecutar este bloque, se
observa cuántos empleados hay y cuántos fueron eliminados, y luego se deshace cualquier
cambio con ROLLBACK.

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.

El bloque modificado incluye excepciones definidas por el usuario para manejar casos
específicos: una excepción se lanza si no hay empleados en el departamento
(e_no_employees) y otra si no se eliminan empleados (e_no_deletions). En la sección
EXCEPTION, se manejan estas excepciones y se muestra un mensaje apropiado. Al
ejecutar este bloque, se muestra un mensaje si no hay empleados o si no se eliminan
empleados, además de manejar cualquier otro error con una excepción general.
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.

El bloque se modifica para usar RAISE_APPLICATION_ERROR con números de error


personalizados (-20203 y -20204) en lugar de excepciones definidas por el usuario. Esto
lanza errores específicos si no hay empleados o si no se eliminan empleados, mostrando
mensajes de error personalizados. La sección EXCEPTION maneja cualquier otro error con
la excepción OTHERS, mostrando el código y el mensaje de error. Al ejecutar este bloque,
se observa el manejo de errores con números de error personalizados y mensajes
específicos.

You might also like