Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Script or Tip to add login
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

United Kingdom
115 Posts

Posted - 09/16/2013 :  12:27:36  Show Profile  Reply with Quote

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
Flowing Fount of Yak Knowledge

1834 Posts

Posted - 09/16/2013 :  19:05:11  Show Profile  Reply with Quote
Within a server can be solved with the undocumented sproc sp_MSforeachdatabase. Here's a link to its usage:

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

Posting Yak Master

United Kingdom
115 Posts

Posted - 09/18/2013 :  06:53:28  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000