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

Handling Exceptions: Paste The Modified Code Here

This document discusses handling exceptions in PL/SQL code. It provides 3 examples of code blocks that could result in exceptions and asks how to modify the code to properly handle the exceptions. The examples include a SELECT statement that could return too many rows, an INSERT statement that results in a unique constraint violation, and a SELECT that may return no rows or too many rows depending on the input parameter. The requested modifications are to add exception handlers to catch and handle the potential errors.

Uploaded by

Cody Benfield
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Handling Exceptions: Paste The Modified Code Here

This document discusses handling exceptions in PL/SQL code. It provides 3 examples of code blocks that could result in exceptions and asks how to modify the code to properly handle the exceptions. The examples include a SELECT statement that could return too many rows, an INSERT statement that results in a unique constraint violation, and a SELECT that may return no rows or too many rows depending on the input parameter. The requested modifications are to add exception handlers to catch and handle the potential errors.

Uploaded by

Cody Benfield
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

3 points for each

Handling Exceptions
1.
DECLARE
v_jobid employees.job_id%TYPE;
BEGIN
SELECT job_id
INTO v_jobid
FROM employees
WHERE department_id = 80;
END;
In Application Express modify the code to fix the problem with an EXCEPTION Handler
Paste the modified code here
DECLARE
v_jobid employees.job_id%TYPE;
BEGIN
SELECT job_id
INTO v_jobid
FROM employees
WHERE department_id = 80;
exception
When too_many_rows then
DBMS_OUTPUT.PUT_LINE (' Your select statement retrieved
multiple rows. Consider using a cursor.');
END;

2. Run the following PL/SQL block, which tries to insert a new row (with department_id = 50)
into the departments table.
BEGIN
INSERT INTO departments(department_id, department_name, manager_id, location_id)
VALUES(50, 'Management Information Systems', 100,1500);
DBMS_OUTPUT.PUT_LINE('The new department was inserted');
END;
A. What happened when you run this code and why?

B.

In Application Express modify the code to include exception WHEN OTHERS.An


exception has occurred
Paste the modified code here
BEGIN
INSERT INTO departments(department_id, department_name, manager_id, location_id)
VALUES(50, 'Management Information Systems', 100,1500);
DBMS_OUTPUT.PUT_LINE('The new department was inserted');
Exception
When others then
DBMS_OUTPUT.PUT_LINE('An exception has occurred');
END;

3. Run the following PL/SQL block, which tries to SELECT all the employees in a specific
department. Run it three times, using department_ids 10, 20 and 30. What happens and why?
DECLARE
v_employee_id employees.employee_id%TYPE;
v_last_name employees.last_name%TYPE;
BEGIN
SELECT employee_id, last_name INTO v_employee_id, v_last_name
FROM employees
WHERE department_ID = 10;
END;
A. What happens and why?
No data in one and too many rows on the other
B. In Application Express modify the code to include exceptions to handle errors ALL errors
Paste the modified code here

DECLARE
v_employee_id employees.employee_id%TYPE;
v_last_name
employees.last_name%TYPE;
BEGIN
SELECT employee_id, last_name INTO v_employee_id, v_last_name
FROM employees
WHERE department_ID = 20;
exception
When too_many_rows then
DBMS_OUTPUT.PUT_LINE (' Your select statement retrieved
multiple rows. Consider using a cursor.');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('no data');
END;

You might also like