Multi-server scripts with SSMS using SQLCMD mode

SSMS is the most common tool used by SQL Developers or DBA’s but unfortunately there are some features that are barely known, SQLCMD mode is not the exception and that’s why I decided to follow up in this topic.

You may want to check one of this previous blog post, where you will find instructions how to enable SQLCMD mode and some examples that could help you to become familiar with it.

One of my favorite things about SQLCMD mode, is that allows you to connect to multiple servers using the same SSMS editor window. That means, we can run the same T-SQL script on multiple servers one after the other.

All SQLCMD mode commands starts with colon sign (:), in this case what we want to do is to connect to multiple servers so we need to make use of the :CONNECT command.

Imagine you have to backup and restore a database from production to a development environment and because this is an on demand task you want to have a handy script to run it every time needed, let’s give it a try.

-- Connecting to source server, taking full and log backup
:CONNECT Instance1
USE [master]
GO

BACKUP DATABASE [ProdDB] TO DISK=N’\\Instance2\D$\ProdDB.bak’ WITH NOFORMAT, NOINIT,
NAME =N’ProdDB-Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 5
GO

BACKUP LOG [ProdDB] TO DISK=N’\\Instance2\D$\ProdDB.trn’ WITH NOFORMAT, NOINIT,
NAME =N’ProdDB-Log Database Backup’, SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 5
GO

-- Connecting to target server, restoring full and log backup
:CONNECT Instance2
USE [master]
GO

RESTORE DATABASE [DevDB] FROM DISK=N’D:\ProdDB.bak’ WITH FILE=1,
MOVE N’ProdDB_data’ TO ‘E:\DATA\DevDB_data.mdf’,
MOVE N’ProdDB_main_01’ TO ‘F:\DATA\DevDB_main_01.ndf’,
MOVE N’ProdDB_log’ TO ‘G:\DATA\DevDB_log.ldf’,
NORECOVERY, NOUNLOAD, REPLACE, STATS = 5
GO
RESTORE LOG [DevDB] FROM DISK=N’D:\ProdDB.trn’ WITH FILE=1,NORECOVERY, NOUNLOAD, REPLACE, STATS = 5
GO

-- Recovering database
RESTORE DATABASE [DevDB] WITH RECOVERY
GO

As you can see from the example above, just using the :CONNECT functionality in SQLCMD mode allows me to run mixed operations between servers. In this particular example I took a full and log backup of a database, then without leaving my query window (session) on SSMS I connected to the target server and restored both files.

This is just a simple example, you can even run the same script in a large collection of servers to make repetitive admin tasks easier.

Stay tuned for more DBA mastery tips!

4 thoughts on “Multi-server scripts with SSMS using SQLCMD mode

  1. Eric in Sacramento

    Thanks, Carlos, I need more info on SQLCMD mode. This was helpful. Would you happen to know if it is possible to create a recordset (such as a temp table or table variable, though I don’t see how these would work) from Instance1 and use it on Instance2 within the same script?

    Reply
    1. DBA Master Post author

      Yes and no, it depends of what you want to achieve. There is an option to “dump” the information into a flat file, also when using CMS (very similar) you can create a temp table locally or either way on the target server.

      In case you want to discuss further send me an email (dbamastery@gmail.com), I will be more than happy to work with you.

      Reply
  2. jorge gomes

    hello,
    i think “SQLCMD” is fantastic but, in this case, why don’t you use “linked server”?

    With a configured linked server you can execute things on both servers without exit from the same window…or not?

    Thank you,
    JG

    Reply
    1. DBA Master Post author

      With linked servers you can remote queries as you described the problem with this feature is that you need to have such object configured previously, thus you need to invest time creating them also maintaining them (in case of login changes). SQLCMD mode it’s a very easy way to do it without the hassle of pre-configuring things.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.