NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527122] |
Sat, 15 October 2011 05:12  |
 |
ninan
Messages: 163 Registered: June 2011 Location: Noida
|
Senior Member |
|
|
Have a doubt on how NO_DATA_FOUND works.
I could see that this exception happens only on the second select statement not on the select statement with COUNT(1).
I cannot catch exception for SELECT COUNT(1) since the count will be 0 , is there any other exception to catch this , so that we can avoid further coding like....
----
IF COUNT()>0 THEN....
----
Life would have been easy if ORACLE introduced some exception if COUNT did'nt return any records ...i.e., for COUNT =0
declare
n number;
trace_error varchar2(100);
begin
select count(1) into n from ITEMS where item='ORACLE';
trace_error := ' ERROR FOR COUNT';
select cost1 into n from ADMIN_COSTPL_OWNER.ITEM_COST_C10000 where item='ORACLE';
trace_error := trace_error || ', '|| ' ERROR FOR SELECT';
exception when no_data_found then
dbms_output.put_line( trace_error);
end;
|
|
|
Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527124 is a reply to message #527122] |
Sat, 15 October 2011 05:27   |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
no_data_found gets raised when there is no data found in the case of a select into that requires the existence of exactly one row.
It makes no sense to raise an exception when count is "0". If I tell Oracle to count something then "0" is a perfectly reasonable answer, not an exception.
If you want to check if there is AT LEAST one entry you could use
select 1 into n from ITEMS where item='ORACLE' and rownum =1;
which would then raise no_data_found when there are no "ORACLE" items, and also do it a lot faster than doing it by counting them all.
[Updated on: Sat, 15 October 2011 05:31] Report message to a moderator
|
|
|
|
|
|
|
|
Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527144 is a reply to message #527140] |
Sat, 15 October 2011 12:04   |
 |
Michel Cadot
Messages: 68767 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
No, this (count(*) that scans all colums) has NEVER existed, NEVER, in any version.
All what you read saying that was, is and will always be wrong.
Do you think that Oracle designers were stupid?
"*" is a keyword like "PARTITION" and like "PARTITION" his meaning is different depending on the context.
Regards
Michel
[Updated on: Sat, 15 October 2011 12:06] Report message to a moderator
|
|
|
|
|