| Author |
Topic |
|
optimist
Starting Member
1 Post |
Posted - 2006-11-20 : 11:26:11
|
| Hello AllPlease can anyone advice me how I can fetch list of all tables which have no records in it in sql serverThanks |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-20 : 11:34:36
|
This should do itSELECT o.[name] FROM sysindexes iINNER JOIN sysobjects o on i.[id] = o.[id] WHERE indid < 2 and [rows] = 0 and o.[type] = 'U' |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-11-20 : 11:42:18
|
of course this would require for all tables to have indexes and their statistics to be up to date.you could also use an undocumented stored procedure:exec sp_msforeachtable 'select ''?'', count(*) from ?'Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2006-11-20 : 11:45:30
|
Here's one way...you may need to add the table owner thoughUSE NorthwindGOSET NOCOUNT ONCREATE TABLE x (TABLE_NAME sysname, [Count] int)GODECLARE @sql varchar(8000), @TABLE_NAME sysnameSELECT @TABLE_NAME = MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TablesWHILE @TABLE_NAME IS NOT NULL BEGIN SELECT @sql = 'INSERT INTO x(TABLE_NAME, [Count]) SELECT '''+@TABLE_NAME+''', COUNT(*) FROM [' + @TABLE_NAME+']' EXEC(@sql) SELECT @TABLE_NAME = MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME > @TABLE_NAME ENDSELECT TABLE_NAME, [Count] FROM x -- WHERE [Count] = 0GOSET NOCOUNT OFFDROP TABLE xGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-20 : 12:26:28
|
quote: of course this would require for all tables to have indexes and their statistics to be up to date.
No, that's not true. All tables have an entry in sysindexes, even those that do not have an index. As for statistics being up to date, just run sp_updatestats before you run the query. Your stats should be up to date anyway - and the row count is pretty reliable anyway, it usually only is wrong if you've performed bulk operations or other non standard operations on the table. If you have some large tables, running a count(*) on each of them will be a lot slower than querying sysindexes. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-11-20 : 12:27:55
|
cool. thanx.Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sanjayanthan
Starting Member
5 Posts |
Posted - 2006-11-21 : 18:51:55
|
| Expert answer see the below linkhttp://searchsqlserver.techtarget.com/expert/KnowledgebaseAnswer/0,289625,sid87_gci1188953,00.htmlsanjayanthan |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-21 : 21:52:41
|
quote: Originally posted by sanjayanthan Expert answer see the below linkhttp://searchsqlserver.techtarget.com/expert/KnowledgebaseAnswer/0,289625,sid87_gci1188953,00.htmlsanjayanthan
This uses dynamic SQL completely unnecessarily, rather use one of the ways you see here - so much for the "expert" answer. |
 |
|
|
|