PL SQL ORA-01422 exact fetch returns more than requested number of rows

0 votes

I am getting this error:

DECLARE
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 11

Code.

DECLARE
    rec_ENAME EMPLOYEE.ENAME%TYPE;
    rec_JOB EMPLOYEE.DESIGNATION%TYPE;
    rec_SAL EMPLOYEE.SALARY%TYPE;
    rec_DEP DEPARTMENT.DEPT_NAME%TYPE;
BEGIN       
    SELECT EMPLOYEE.EMPID, EMPLOYEE.ENAME, EMPLOYEE.DESIGNATION, EMPLOYEE.SALARY,  DEPARTMENT.DEPT_NAME 
    INTO rec_EMPID, rec_ENAME, rec_JOB, rec_SAL, rec_DEP 
    FROM EMPLOYEE, DEPARTMENT 
    WHERE EMPLOYEE.SALARY > 3000;

    DBMS_OUTPUT.PUT_LINE ('Employee Nnumber: ' || rec_EMPID);
    DBMS_OUTPUT.PUT_LINE ('---------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Name: ' || rec_ENAME);
    DBMS_OUTPUT.PUT_LINE ('---------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Designation: ' || rec_JOB);
    DBMS_OUTPUT.PUT_LINE ('----------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Salary: ' || rec_SAL);
    DBMS_OUTPUT.PUT_LINE ('----------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Department: ' || rec_DEP);

END;
/

Can someone please help me with this?

Aug 19, 2022 in Database by Kithuzzz
• 38,000 points
1,287 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

If a SELECT INTO command returns more than one row, an error will be raised. You will receive a no_data_found exception if it returns zero rows. An exception titled too many rows will be thrown if it returns more than one row. You don't want a SELECT INTO statement here unless you are certain that there will always be one employee whose income is higher than $3,000 per year. Assuming you meant to perform a proper join between the two tables rather than a Cartesian product, and that there is a departmentID field in both tables, you most certainly want to use a cursor to traverse over (perhaps) many rows of data.

BEGIN
  FOR rec IN (SELECT EMPLOYEE.EMPID, 
                     EMPLOYEE.ENAME, 
                     EMPLOYEE.DESIGNATION, 
                     EMPLOYEE.SALARY,  
                     DEPARTMENT.DEPT_NAME 
                FROM EMPLOYEE, 
                     DEPARTMENT 
               WHERE employee.departmentID = department.departmentID
                 AND EMPLOYEE.SALARY > 3000)
  LOOP
    DBMS_OUTPUT.PUT_LINE ('Employee Nnumber: ' || rec.EMPID);
    DBMS_OUTPUT.PUT_LINE ('---------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Name: ' || rec.ENAME);
    DBMS_OUTPUT.PUT_LINE ('---------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Designation: ' || rec.DESIGNATION);
    DBMS_OUTPUT.PUT_LINE ('----------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Salary: ' || rec.SALARY);
    DBMS_OUTPUT.PUT_LINE ('----------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE ('Employee Department: ' || rec.DEPT_NAME);
  END LOOP;
END;

I'm assuming that you are just learning PL/SQL as well. In real code, you'd never use dbms_output like this and would not depend on anyone seeing data that you write to the dbms_output buffer.

answered Aug 20, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Database

0 votes
1 answer

Number of rows affected by an UPDATE in PL/SQL

You can try using the following query: SELECT ...READ MORE

answered Feb 14, 2022 in Database by Vaani
• 7,070 points
1,475 views
0 votes
1 answer

SQL error "ORA-01722: invalid number"

When an effort is made to convert ...READ MORE

answered Feb 15, 2022 in Database by Neha
• 9,020 points
3,371 views
0 votes
1 answer

Maximum number of rows of CSV data in excel sheet

The maximum number of characters per cell ...READ MORE

answered Apr 6, 2022 in Database by gaurav
• 23,260 points
9,011 views
0 votes
0 answers

SQL to find the number of distinct values in a column

In a column, I can choose each ...READ MORE

Aug 15, 2022 in Database by Kithuzzz
• 38,000 points
1,109 views
0 votes
0 answers

ORA-06502: PL/SQL: numeric or value error: character string buffer too small

I experimented with the following code in ...READ MORE

Aug 22, 2022 in Database by Kithuzzz
• 38,000 points
6,895 views
0 votes
0 answers

SQL-Server: The backup set holds a backup of a database other than the existing

My attempt to restore a SQL Server ...READ MORE

Aug 30, 2022 in Database by Kithuzzz
• 38,000 points
709 views
0 votes
1 answer

How to display databases in Oracle 11g using SQL*Plus

A MySQL "database" can be compared to ...READ MORE

answered Sep 12, 2022 in Database by narikkadan
• 63,600 points
3,654 views
0 votes
1 answer

Oracle PL/SQL - ORA-01403 "No data found" when using "SELECT INTO"

Just populate the field directly, using ordering ...READ MORE

answered Sep 18, 2022 in Database by narikkadan
• 63,600 points
6,253 views
0 votes
1 answer
0 votes
1 answer

How to take user input in livesql.oracle.com in PLSQL?

To create a procedure with a parameter ...READ MORE

answered Feb 7, 2022 in Database by Vaani
• 7,070 points
4,434 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP