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 !
Best Answer
Naming your parameter the same way you named your column is quite unfortunate.
In the above statement, typing
id
in lowercase orID
in uppercase makes no difference, it is case insensitive. Just because you named your PL/SQL parameterID
(in uppercase), does not mean the database will use it when you typeID
(in uppercase) in the above. Even if theid
incustomers
has unique values, the aboveSELECT
returns all* rows from the table, because theid
column precedes theID
parameter, andid
always* equalsID
(*except forNULLs
).You could just simply use a different name for your parameter, for example I usually use the
p_
prefix for them, so yourID
parameter could be changed toP_ID
: