alternatively, you could use dynamic SQL to avoid using the MS system tables, and the MS undocumented process (which, btw, will return values for all tables, and not just he set you may want.have a look at the following:SET NOCOUNT ON DECLARE @TablesToCount TABLE (TableNames SYSNAME)INSERT INTO @TablesToCount SELECT 'table1'UNION SELECT 'table2'UNION SELECT 'table3'UNION SELECT 'table4'UNION SELECT 'table5'UNION SELECT 'table6'UNION SELECT 'table7'UNION SELECT 'table8'UNION SELECT 'table9'UNION SELECT 'table10'--SELECT [Name] FROM INFORMATION_SCHEMA.TABLES WHERE [Table_Name] in ('')DECLARE @SQLCommand VARCHAR (8000), --the script can be quite large. @Debug INTSET @SQLCommand = ''SET @debug = 1SELECT @SQLCommand=@SQLCommand+'SELECT COUNT(*) as [CountOutPut_'+TableNames+'] FROM ['+TableNames+'];' FROM @TablesToCountIF @Debug = 1 SELECT @SQLCommandIF @Debug = 0 EXEC (@SQLCommand)
While this is quite ugly (the hardcoded table), you will note I've commented out the section which allows you to use the SQL approved schema views (these will remain statis across different versions of SQL, as opposed to system tables, which MS can change freely. The advantage of that approac (as with Srinika's posting) is you can reduce the set of table - you don't ahve to report on all tables in the database. If you want to report on all tables, you could simply remove the where criteria, and query the INFORMATION_SCHEMA.TABLES view.in debug mode, this generates the following as a SQL statement:SELECT COUNT(*) as [CountOutPut_table1] FROM [table1];SELECT COUNT(*) as [CountOutPut_table10] FROM [table10];SELECT COUNT(*) as [CountOutPut_table2] FROM [table2];SELECT COUNT(*) as [CountOutPut_table3] FROM [table3];SELECT COUNT(*) as [CountOutPut_table4] FROM [table4];SELECT COUNT(*) as [CountOutPut_table5] FROM [table5];SELECT COUNT(*) as [CountOutPut_table6] FROM [table6];SELECT COUNT(*) as [CountOutPut_table7] FROM [table7];SELECT COUNT(*) as [CountOutPut_table8] FROM [table8];SELECT COUNT(*) as [CountOutPut_table9] FROM [table9];
In the execute mode, it would execute those, and run the result to output.in fact, for your purposes, the table variable is also unnecesary. You could go with:SET NOCOUNT ON DECLARE @SQLCommand VARCHAR (8000), --the script can be quite large. @Debug INTSET @SQLCommand = ''SET @debug = 1SELECT @SQLCommand=@SQLCommand+'SELECT COUNT(*) as [CountOutPut_'+Table_Name+'] FROM ['+Table_Name+'];' FROM INFORMATION_SCHEMA.TABLES --where Table_Name in ('') or Table_Name like '' -- this is left pto your needIF @Debug = 1 SELECT @SQLCommandIF @Debug = 0 EXEC (@SQLCommand)
which generated:SELECT COUNT(*) as [CountOutPut_spt_fallback_db] FROM [spt_fallback_db];SELECT COUNT(*) as [CountOutPut_spt_fallback_dev] FROM [spt_fallback_dev];SELECT COUNT(*) as [CountOutPut_spt_fallback_usg] FROM [spt_fallback_usg];SELECT COUNT(*) as [CountOutPut_spt_monitor] FROM [spt_monitor];SELECT COUNT(*) as [CountOutPut_spt_values] FROM [spt_values];SELECT COUNT(*) as [CountOutPut_MSreplication_options] FROM [MSreplication_options];
on my master db.PS... w00t - this is my 1000'th post.CiaO*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here!