Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using count(*) for flag indicators

Author  Topic 

rosejr
Starting Member

12 Posts

Posted - 2007-08-15 : 14:57:58
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?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-15 : 15:03:20
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

pootle_flump

1064 Posts

Posted - 2007-08-15 : 18:08:05
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

rosejr
Starting Member

12 Posts

Posted - 2007-08-16 : 09:57:57
Thanks guys that is what I was looking for. I was trying to avoid table scans.
Go to Top of Page
   

- Advertisement -