0

I want to write PL/SQL Procedure that updates salary which is less than 2000. I wrote this procedure.And when i call it with integer id 'ORA-01422: exact fetch returns more than requested number of rows' error is thrown by TOAD. My procedure is like below:

DECLARE
PROCEDURE update_salary(ID customers.id%type) is
 c_sal customers.salary%type;
BEGIN
 SELECT salary 
 INTO c_sal 
 FROM customers 
 WHERE id = ID;
 dbms_output.put_line ('Before update operation salary is:' || c_sal);
 --dbms_output.put_line ('Parameter :' || ID);
 IF (c_sal <= 2000) THEN
 UPDATE customers 
 SET salary = salary + 1000
 WHERE id = ID;
 dbms_output.put_line ('Salary updated');
 END IF;
 SELECT salary 
 INTO c_sal 
 FROM customers 
 WHERE id=ID;
 dbms_output.put_line ('After update operation salary is:' || c_sal);
END;
BEGIN
update_salary(1);
END;
/

I print parameter id with dbms_output. The parameter is coming correctly. How can i fix this error !

asked Nov 15, 2015 at 14:04

2 Answers 2

0

Naming your parameter the same way you named your column is quite unfortunate.

 SELECT salary 
 INTO c_sal 
 FROM customers 
 WHERE id = ID;

In the above statement, typing id in lowercase or ID in uppercase makes no difference, it is case insensitive. Just because you named your PL/SQL parameter ID (in uppercase), does not mean the database will use it when you type ID (in uppercase) in the above. Even if the id in customers has unique values, the above SELECT returns all* rows from the table, because the id column precedes the ID parameter, and id always* equals ID (*except for NULLs).

You could just simply use a different name for your parameter, for example I usually use the p_ prefix for them, so your ID parameter could be changed to P_ID:

DECLARE
PROCEDURE update_salary(P_ID customers.id%type) is
 c_sal customers.salary%type;
BEGIN
 SELECT salary 
 INTO c_sal 
 FROM customers 
 WHERE id = P_ID;
 dbms_output.put_line ('Before update operation salary is:' || c_sal);
 --dbms_output.put_line ('Parameter :' || ID);
 SELECT salary 
 INTO c_sal
 FROM customers
 WHERE id = P_ID;
 IF (c_sal <= 2000) THEN
 UPDATE customers 
 SET salary = salary + 1000
 WHERE id = P_ID;
 dbms_output.put_line ('Salary updated');
 END IF;
 SELECT salary 
 INTO c_sal 
 FROM customers 
 WHERE id=P_ID;
 dbms_output.put_line ('After update operation salary is:' || c_sal);
END;
BEGIN
update_salary(1);
END;
/
answered Nov 15, 2015 at 15:15
0
-1

This mean query:

SELECT salary 
 INTO c_sal 

return more than 1 row check the table for count records with ID = 1

if ID by any legal reason non unique - need make LOOP

answered Nov 15, 2015 at 15:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.