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
 General SQL Server Forums
 New to SQL Server Programming
 Checking if a table is empty using count

Author  Topic 

albaker
Starting Member

7 Posts

Posted - 2007-09-10 : 05:21:37
Hi,

I want to use something like select count(*) from table name = 0; to check whether a table is empty,

is this possible?

Thanks for any info.

Al.

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2007-09-10 : 05:56:57
Try this, if all statistics are updated then only it wll give exact no of rows


Select so.[Name] as 'TblName', si.rows as 'Count'
From sys.sysobjects so
Inner join sys.sysindexes si On so.id = si.id
Where so.xtype = 'u'
and si.indid < 2
Order By si.rows

else u have to use dynamic sql with loop
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-10 : 06:11:37
[code]IF (SELECT COUNT(*) FROM Table1) = 0
THEN
....
ELSE
....[/code]
[code]IF EXISTS (SELECT * FROM Table1)
THEN
....
ELSE
....[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

albaker
Starting Member

7 Posts

Posted - 2007-09-10 : 06:35:40
Thank you.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-11 : 06:05:06
Note that IF EXISTS method is faster than other methods to check existance of the data

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -