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.
| Author |
Topic |
|
morphias0
Starting Member
20 Posts |
Posted - 2006-02-10 : 10:13:24
|
| Heres some background, I've been working on a script to backup all the databases on a server. While i dont mind typing out all the databases on the server I would like to have this code condensed as much as I can.DECLARE @1DB varchar(100), @2DB varchar(100), @3DB varchar(100), @4DB varchar(100), @5DB varchar(100), @6DB varchar(100), @ServerName varchar(100), @dbcount int, @database varchar(100)SET @ServerName = 'Wisepkg1'set @1DB = 'Wise Services Database'SET @2DB = 'Workbench Database'SET @3DB = 'MASTER'SET @4DB = 'MODEL'SET @5DB = 'MSDB'SET @6DB = 'Software Manager Database'set @dbcount = 6--Starting Loopwhile @dbcount <> 0 begin--Trying to compact from here on--Setting up the databases namesIF @DBCOUNT = 1 BEGINSET @Database = @1DBENDelse IF @DBCOUNT = 2BEGINSET @Database = @2DBENDelse IF @DBCOUNT = 3BEGINSET @Database = @3DBENDelse IF @DBCOUNT = 4BEGINSET @Database = @4DBENDelse IF @DBCOUNT = 5BEGINSET @Database = @5DBENDelse IF @DBCOUNT = 6BEGINSET @Database = @6DBEND-----------------------------------------Basically i want to have something like this:set @database = '@' + cast(dbcount as varchar(100)) + 'DB'----------------------------------------------------------Having code like this would cut all the if statements and make it into one line. But it wont work, it will set @database to "@1DB" as a string and i want it to set it to @1DB the variable....IS THIS POSSIBLE? IS THERE A BETTER WAY?Thanks for the help |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
morphias0
Starting Member
20 Posts |
Posted - 2006-02-10 : 10:49:57
|
| If i were to use something like that i would have to run the stored procedure on all the servers, i would also have to create the admin db on all the servers as well, isnt that correct? The thing is, we hardly ever get new db on a server, if we decide to get a new db we will usually get a new server to have it run on. So i dont really mind typing in the db name for each server. I would really just like what i have to work instead of going through that. Is it possible for what i want. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-02-10 : 11:22:21
|
I don't think this is any better than what you have but...There are a lot of ways to loop through a list of DBs. Here's one:declare @database varchar(100)select @database = 'master'while @database is not nullbegin --do you processing here print @database select @database = min(name) from master..sysdatabases where name in ('Model' ,'msdb' ,'Workbench') --etc... and name > @databaseendBe One with the OptimizerTG |
 |
|
|
|
|
|
|
|