Home » SQL & PL/SQL » SQL & PL/SQL » NO_DATA_FOUND Exception FOR SELECT COUNT(1) (11g, Windows XP)
NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527122] Sat, 15 October 2011 05:12 Go to next message
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 Go to previous messageGo to next message
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 #527127 is a reply to message #527124] Sat, 15 October 2011 09:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
... and do NOT use COUNT(1), use COUNT(*)
Using count(1) just shows you don't understand SQL.

Regards
Michel
Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527131 is a reply to message #527127] Sat, 15 October 2011 09:59 Go to previous messageGo to next message
ninan
Messages: 163
Registered: June 2011
Location: Noida
Senior Member
Ok. Can you explain ? I thought COUNT(1) is good performance wise. How does it make a difference. Both are counting the rows.only diff. being COUNT(*) have to scan all the columns.

Correct me if I am wrong.
Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527133 is a reply to message #527131] Sat, 15 October 2011 10:05 Go to previous messageGo to next message
ninan
Messages: 163
Registered: June 2011
Location: Noida
Senior Member
Quoting a forum answer from an oracle employee on Metalink:
Subject: Re : Performance while using ROWNUM v/s COUNT
Hi,

If the intention is only to count the number of Records matching the criteria then you could as
well use :

count(1) ..

This is the fastest as the numberical literal is the fastest to be summed up.

Max rownum will lead to implicit ordering while count(id) will also be effecient as the value is a
primary key though not as good as count(1). "


Quote:


Using count(1) just shows you don't understand SQL.



Please give proof. Has Oracle documented this , I have not heard any Oracle professional telling this over many years. First time from you. Enlightenment , Nirvana??

Base of your theory ??

COUNT(1) will not yield any result.?

COUNT(1) will give wrong result ?

COUNT(1) will not work at all ??
Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527134 is a reply to message #527131] Sat, 15 October 2011 10:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
1/ There is NO difference internally count(1) is converted to COUNT(*)
2/ As I said using count(1) proves you don't understand and indeed you don't understand. COUNT(*) does NOT count and scan all the columns, it counts the rows, ONLY the rows, NOTHING BUT the rows, not even 1. So performances wise... back to 1/.

SQL> set autotrace traceonly explain
SQL> select empno from emp group by empno having count(1) > 0;

Execution Plan
----------------------------------------------------------
Plan hash value: 1668054362

--------------------------------------------------------------------------------
| Id  | Operation             | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |        |    14 |   182 |     1   (0)| 00:00:01 |
|*  1 |  FILTER               |        |       |       |            |          |
|   2 |   SORT GROUP BY NOSORT|        |    14 |   182 |     1   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN    | EMP_PK |    14 |   182 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(COUNT(*)>0)


See the last line.

Regards
Michel
Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527140 is a reply to message #527134] Sat, 15 October 2011 10:48 Go to previous messageGo to next message
ninan
Messages: 163
Registered: June 2011
Location: Noida
Senior Member
I still disagree to your point. This misconception prevailed for a long since Oracle Version 7 days.

I have read that during that time there was a difference , and later versions from Oracle 8.0, the parser was changed, so that the difference between two no more exist.

[Updated on: Sat, 15 October 2011 10:48]

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 Go to previous messageGo to next message
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

Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527151 is a reply to message #527133] Sat, 15 October 2011 14:30 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
If you click on the following link, you will see that Oracle vice president Tom Kyte shows that count(1) and count(*) are currently the same. In the same thread, he also mentions that, back in Oracle 7, when they were different, count(1) was slower.

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1156159920245

[Updated on: Sat, 15 October 2011 14:30]

Report message to a moderator

Re: NO_DATA_FOUND Exception FOR SELECT COUNT(1) [message #527214 is a reply to message #527151] Mon, 17 October 2011 01:08 Go to previous message
Baranor
Messages: 83
Registered: September 2011
Location: Netherlands
Member
Interesting. Always something to learn Smile
Previous Topic: Simple Oracle Query
Next Topic: DDL = auto commit?
Goto Forum:
  


Current Time: Sat Aug 23 23:38:52 CDT 2025