Home » SQL & PL/SQL » SQL & PL/SQL » Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows
Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130200] Thu, 28 July 2005 13:29 Go to next message
welchaz
Messages: 23
Registered: April 2005
Location: Tucson, AZ
Junior Member
I am surprised that when the ORA-01422 error is encountered, that the value of the variable being selected "INTO" still gets populated. See the following simple example (this is Oracle 8.1.7.4):

1 declare
2 v_crew TWMWORKPACKET.CD_CREW%TYPE;
3 begin
4 begin
5 SELECT distinct cd_crew
6 INTO v_crew
7 FROM twmworkpacket
8 WHERE cd_dist = 'MAT'
9 AND cd_wr = 142653
10 AND cd_crew IS NOT NULL;
11 exception
12 when others then
13 dbms_output.put_line(SQLERRM);
14 end;
15 DBMS_OUTPUT.PUT_LINE('V crew = ' || v_crew);
16 end;
17 /
ORA-01422: exact fetch returns more than requested number of rows
V crew = POLHLR

PL/SQL procedure successfully completed.

I expected that when an exception is encountered, the value of V_CREW would remain null as initialized. However, it appears to be populating the variable with the first crew it comes across, then erroring. Is this how it was intented to work?

Thanks in advance
Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130214 is a reply to message #130200] Thu, 28 July 2005 16:42 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Oracle 7 = value was left NULL
Oracle 8/9 = value was set to the first value fetched
Oracle 10 = value is once again left NULL

Isn't consistency wonderful?
Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130217 is a reply to message #130214] Thu, 28 July 2005 17:27 Go to previous messageGo to next message
welchaz
Messages: 23
Registered: April 2005
Location: Tucson, AZ
Junior Member
Wow! Thanks for letting me know.
Now pardon me...I have a lot of production code to check for my erroneous assumption!
Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130497 is a reply to message #130200] Sun, 31 July 2005 14:43 Go to previous messageGo to next message
oraclejo
Messages: 50
Registered: July 2005
Location: Ammar
Member
regardless of the value retrieved, the exception too_many_rows is an indication that you are trying to populate a single value variable with a set of values. If you are expecting that, you should be writing a Cursor in scrolling through it, if you are not, then you should stop immediately and raise an error, where the value fetched is really irrelevent, because it could mean that your data is not what you are expecting (duplicates)

Ammar Sajdi

www.e-ammar.com
Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130625 is a reply to message #130497] Mon, 01 August 2005 11:07 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Why would you have to stop and raise an error? Maybe the too_many_rows exception is a possibility, and you just want to continue processing down a different path?

It's not necessarily a "raise the red flag" exception and you cannot make a general statement about how it should be handled.
Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130642 is a reply to message #130200] Mon, 01 August 2005 15:23 Go to previous messageGo to next message
oraclejo
Messages: 50
Registered: July 2005
Location: Ammar
Member
In this case, you are working with a set. The proper way to work with a set is Explicit Cursor and not Implicit (Select Into )

Regards

Ammar Sajdi
Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130649 is a reply to message #130642] Mon, 01 August 2005 16:10 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
A set? Where do you see a set? The query is expecting a single distinct value.

The "proper" way is an explicit cursor? You won't get much support on that around here. Why is code to open/fetch/check/close more "proper"? Not only do implicit cursors require less code, they actually run slightly faster than explicit cursors. But you knew that, right?

[Updated on: Mon, 01 August 2005 16:10]

Report message to a moderator

Re: Unexpected behaviour of ORA-01422: exact fetch returns more than requested number of rows [message #130840 is a reply to message #130200] Tue, 02 August 2005 17:36 Go to previous message
mchadder
Messages: 224
Registered: May 2005
Location: UK
Senior Member
It's the 3x more recursive SQL stats that give away just how much faster explicit cursors are that implicit.... Wink

(tongue firmly in cheek.... is there a smiley for that one?)

Wow....!
Previous Topic: Monitor Columns
Next Topic: sid through query
Goto Forum:
  


Current Time: Fri Aug 08 10:39:26 CDT 2025