Hi all,Here's my situation:We have about 600 structurally identical tables (I cannot change the setup - it's handled by another department), and I need to produce a report with counts from various fields.What I'm currently doing is getting a list of the tablenames, and then without using a cursor, but a temporary table, looping through the list running dynamic sql to retrieve the counts:declare @Column1 intdeclare @name varchar(60)declare @column2 intDeclare @Count intDeclare @SQL VarChar(3000)Declare @NumberRecords intDeclare @RowCount intcreate table #dbs ( RowID int Identity(1,1), dbname varchar(60),)create table #Results ( [DBName] varchar(60), [Count1] int, [Count2] int, [Count3] int, [Count4] int, [Count5] int, [Count6] int, [Count7] int, [Count8] int, [Count9] int, [Count10] int, [Count11] int, [Count12] int,)INSERT INTO #dbsSELECT db from dblistSET @NumberRecords = @@ROWCOUNTSET @RowCount = 1WHILE @RowCount <= @NumberRecordsBEGINSELECT @Name = db from #dbs where rowID = @RowCount Select @SQL = 'Select CAST(''' + @Name + ''' AS VARCHAR(60)) AS ''Db Name'', count(recordsource) as count1, SUM(case when field IN (''9'', ''Q'') then 1 else 0 end) as count2 , SUM(case when field IN (''B'', ''P'') then 1 else 0 end) as count3 , SUM(case when field =''1'' then 1 else 0 end) AS count4, SUM(case when field =''2'' then 1 else 0 end) AS count5, SUM(case when field =''4'' then 1 else 0 end) AS count6, SUM(case when field IN (''6'') then 1 else 0 end) AS count7, SUM(case when field IN (''7'', ''S'') then 1 else 0 end) AS count8, SUM(case when field Not In (''Q'',''B'', ''9'',''1'',''2'', ''4'', ''6'', ''P'', ''7'', ''S'') then 1 else 0 end) AS count9, (Select Count(distinct field2 ) from ' + @name + ' where field2 <> '''') As count10, (Select Count(distinct field3 ) from ' + @name + ' where source2=1) As count11', SUM(case when field4=1 then 1 else 0 end) as count12 from ' + @name Insert #Results Exec(@SQL) Set @RowCount = @RowCount + 1 endgo--select * from #dbsselect * from #Resultsdrop table #resultsdrop table #dbs
There's probably about a million records in all of the tables combined. Maybe more . . .For the entire code block to run in SQL Manager, it takes over 5 min.I'd like to greatly reduce that time.I thought about trying to union all of the tables and then doing a group by - but before I trek down that road, I thought I'd ask if anybody else can see a faster way to do what I'm trying to do.Thank you,Carrie