Sql-server – Specify Connection in Management Studio T-SQL Query

sql-server-2008ssms

When adding users are roles to DB servers I often use the "Script this action" function from the GUI. I then just go to "Connection :: Change Connection" to do the same on my other servers.

Is there a way I can specify the connection in the scripted action so I don't have to do that second Change Connection step?

Best Answer

No way to do this as part of a script from SSMS, but you do have two options.

One thing you can do is use SQLCMD mode and the ::connect command in order to have a script that will connect to multiple servers and run the script. This works well if you save the script for the user and use the :r command to load the script from a file.

Another thing you can do is configure a Central Management Server and then run your script against multiple servers at once.