Oracle’s PL/SQL language has two basic mechanisms for getting data from the database: SELECT and cursors. SELECT is designed to return a single row into local variables; cursors give you the ability to select multiple rows (i.e., a “rowset”) and process them one at a time. When you use SELECT in a PL/SQL block, it’s …
Oracle’s PL/SQL language has two basic mechanisms for getting data from the database: SELECT and cursors. SELECT is designed to return a single row into local variables; cursors give you the ability to select multiple rows (i.e., a “rowset”) and process them one at a time.
When you use SELECT in a PL/SQL block, it’s important to make sure that exactly one row will always be returned by your query. If more than one row is returned, the TOO_MANY_ROWS exception occurs. Listing A shows an example from Oracle’s HR sample schema: There is more than one employee with the last name King, so the script fails.
DECLARE
l_employee_last_name employees.last_name%TYPE;
l_employee_full_name VARCHAR2(52);
BEGIN
l_employee_last_name := 'King';
SELECT last_name || ', ' || first_name
INTO l_employee_full_name
FROM employees
WHERE last_name = l_employee_last_name;
DBMS_OUTPUT.PUT_LINE (l_employee_full_name);
END;
/
There are four ways to make sure your code is safe from this error.
DECLARE
l_employee_last_name employees.last_name%TYPE;
l_employee_full_name VARCHAR2(52);
BEGIN
l_employee_last_name := 'King';
BEGIN
SELECT last_name || ', ' || first_name
INTO l_employee_full_name
FROM employees
WHERE last_name = l_employee_last_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
l_employee_full_name := '(not on file)';
WHEN TOO_MANY_ROWS THEN
l_employee_full_name := '(duplicate name)';
END;
DBMS_OUTPUT.PUT_LINE (l_employee_full_name);
END;
/
Bob Watkins (OCP, MCITP, MCDBA, MCT) is a computer professional with 25 years of experience as a technical trainer, consultant, and database administrator. He is a Senior Consultant and Managing Partner at B. Watkins, a database consulting and training firm in the Dallas / Fort Worth area. Visit Bob’s site.
——————————————————————————————
Get Oracle tips in your inbox
TechRepublic’s free Oracle newsletter, delivered each Wednesday, covers automating Oracle utilities, generating database alerts, solving directed graph problems, and more. Automatically subscribe today!