The following PL/SQL successfully executes as expected:
declare
myCount number;
begin
select count(*)
into myCount
from myTable
where id = 1;
end;
However, the following does not and instead throws this error:
ORA-01422: exact fetch returns more than requested number of rows
declare
myCount number;
myId number := 1;
begin
select count(*)
into myCount
from myTable
where id = myId;
end;
Conceptually, I understand what the error means, but I don’t know how it applies to my situation. All I’ve done is moved the hardcoded value to a variable in the declare block. Why would that affect the results of the select when it’s the exact same number?
Version is Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production.
There is something going on in your system that you haven’t reproduced in this question. If I create a table
myTablein my local system, your code runs without errorIs it possible that there is some difference between the code you’ve posted and the code that you’re actually running?