C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









IF Exist vs Count


Posted Date: 03 Jul 2008    Resource Type: Code Snippets    Category: SQL

Posted By: M. Irfan       Member Level: Gold
Rating:     Points: 4



IF EXIST Vs COUNT(*)

Many times we see that people use count(*) to get the condition that if we have any record like

IF (SELECT COUNT(*) FROM Employee where Salaried =0 ) >0

BEGIN

PRINT 'Yes Got It'

-- Do your calculation here.

END

We are doing some calculation if we get Salaried=0 for at least once. And I know many times we use the same trick.

"The cost for getting the count(*) is that it counts all the data we get by where clause."

Use if exist to reduce this overhead.

IF EXISTS(SELECT * FROM Employee where Salaried =0)

BEGIN
PRINT 'Yes Get it'

-- Do your calculation here.

END

In this as soon as we get the first record further execution stops and it comes out of the if condition with true. If this not get any result then will cost same as above query.



For more details, visit http://matespoint.blogspot.com/2008/03/if-exist-vs-count-sql-server.html




Responses

Author: Satish N Raykar    04 Jul 2008Member Level: Bronze   Points : 1
Thank you providing good information, i will use the same hence forth...


Author: Satyanarayan SushilKumar Bajoria    04 Jul 2008Member Level: Gold   Points : 1
Hi,

Thanks for your valuable tips on If Exist and Count(*).

Keep posting such valuable tips.

Thanks for sharing your knowledge.

Thanks and Regards
S.S.Bajoria


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Exist in SQL  .  Count vs Exist  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Passing data from one sp to other sp
Previous Resource: Uses of GO in SQL
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use