SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using count(*) for flag indicators
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

12 Posts

Posted - 08/15/2007 :  14:57:58  Show Profile  Reply with Quote
Is there anything wrong with using... count(*) from tables when all you are concerned is to check if rows exists or not. Then after getting the count(*) in a variable, you are simply checking the @variable > 1 for an if ... else statement?

Are there any other suggestion to accomplish this?

Cybernetic Yak Master

11752 Posts

Posted - 08/15/2007 :  15:03:20  Show Profile  Visit spirit1's Homepage  Reply with Quote
use exists if you just want to check for existance

Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

Flowing Fount of Yak Knowledge

United Kingdom
1064 Posts

Posted - 08/15/2007 :  18:08:05  Show Profile  Reply with Quote
To expand on spirit1's excellent suggestion - EXISTS is optimised to stop searching the table as soon as it finds the first row that matches whatever criteria you use. COUNT(*) will count every row that meets the criteria which at worst will be a scan of the entire table.
Go to Top of Page

Starting Member

12 Posts

Posted - 08/16/2007 :  09:57:57  Show Profile  Reply with Quote
Thanks guys that is what I was looking for. I was trying to avoid table scans.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000