tonydismukes
Programmer
I'm trying to clean up some highly inefficient ColdFusion code which loops through a long list of databases, running dynamically built SQL queries on each one. I'm considering moving the entire process into a stored procedure and would like to do something like the following:
Unfortunately, this doesn't work, since SQL Server doesn't like the line 'use @theDB'. Is there a syntax which will work?
I know there will be votes for not using the dynamic SQL at all, but rather writing a separate stored procedure for each DB, so that the procedures can be optimized. I'm sort of in favor of that myself. The problem is that we're a small shop currently without a designated DBA, and we've got a number of developers creating databases at once. I might be transitioning to more of a DBA position over the next year, but I'm not sure I currently have the leverage to hold up each new database until I write the needed stored procedures for each one.
So currently my options are
1) move the looping into TSQL, as shown above (if possible)
2) create stored procedures for each DB and call them dynamically from a loop (may not be able to negotiate this sort of procedure at present time)
3) leave the looping in the ColdFusion code, but clean up the CF code to be more efficient (however, currently our ColdFusion server is much more overloaded than our SQL Server, so I wouldn't mind dumping some of the load off of CF and onto SQL)
Any advice is welcome. Thanks in advance.
Code:
/* simplified, but gives basic idea */
use AdminDB
declare @theDB varchar(40)
declare @theTable varchar(40)
declare @fooCount int
declare @fooTotalCount int
declare dbCursor CURSOR for
Select dbName, tableName from TableList
select @fooTotalCount = 0
open dbCursor
fetch next from dbCursor into @theDB, @theTable
while @@FETCH_STATUS = 0
begin
use @theDB
select @fooCount = count(*) from @theTable where status = 'foo'
set @fooTotalCount = fooTotalCount + @fooCount
fetch next from dbCursor into @theDB, @theTable
end
close dbCursor
deallocate dbCursor
/* do stuff with fooTotalCount down here */
Unfortunately, this doesn't work, since SQL Server doesn't like the line 'use @theDB'. Is there a syntax which will work?
I know there will be votes for not using the dynamic SQL at all, but rather writing a separate stored procedure for each DB, so that the procedures can be optimized. I'm sort of in favor of that myself. The problem is that we're a small shop currently without a designated DBA, and we've got a number of developers creating databases at once. I might be transitioning to more of a DBA position over the next year, but I'm not sure I currently have the leverage to hold up each new database until I write the needed stored procedures for each one.
So currently my options are
1) move the looping into TSQL, as shown above (if possible)
2) create stored procedures for each DB and call them dynamically from a loop (may not be able to negotiate this sort of procedure at present time)
3) leave the looping in the ColdFusion code, but clean up the CF code to be more efficient (however, currently our ColdFusion server is much more overloaded than our SQL Server, so I wouldn't mind dumping some of the load off of CF and onto SQL)
Any advice is welcome. Thanks in advance.