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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Script or Tip to add login

Author  Topic 

Analyzer
Posting Yak Master

115 Posts

Posted - 2013-09-16 : 12:27:36
Hi,

Need to write a script that maps a proxy account (already created) to every database with database role db_backupoperator.

Next problem is there are 200 sql instances (2000, 2005, 2008, 2012) so the syntax will be changing.

The proxy account will always be the same but the instance name\databases will change of course. So would need to make the script generic to run within SQL Group Multi Query.

Any ideas the most efficient method to complete this?

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-09-16 : 19:05:11
Within a server can be solved with the undocumented sproc sp_MSforeachdatabase. Here's a link to its usage: http://www.databasejournal.com/features/mssql/article.php/3441031/SQL-Server-Undocumented-Stored-Procedures-spMSforeachtable-and-spMSforeachdb.htm

Syntax can be resolved by first querying the @@Version and using the syntax appropriate to the version.


=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen
Go to Top of Page

Analyzer
Posting Yak Master

115 Posts

Posted - 2013-09-18 : 06:53:28
Hi - thanks for the direction.

Next issue I have is supplying a variable in the command since this script will be run against 100s servers.

Any ideas how I get sp_MSforeachdb to accept my variable. I have tried the below but it fails. Any ideas?


DECLARE @sqlCommand VARCHAR(2000)
declare @svr as varchar(30)
set @svr = 'select @@servername'

SET @sqlCommand =
'USE ; CREATE USER [@svr\shellLocalUser] FOR LOGIN [@svr\shellLocalUser];'

EXEC master..sp_MSForeachdb @sqlCommand
Go to Top of Page
   

- Advertisement -