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!
Carlos Robles is a Solutions Architect at AWS, a former Microsoft Data Platform MVP, a Friend of Redgate, but more than anything a technology lover. He has worked in the database management field on multiple platforms for over ten years in various industries.
He has diverse experience as a Consultant, DBA and DBA Manager. He is currently working as a Solution Architect, helping customers to solve software/infrastructure problems in their on-premise or cloud environments.
Speaker, author, blogger, mentor, Guatemala SQL User group leader. If you don’t find him chatting with friends about geek stuff, he will be enjoying life with his family.
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?
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.
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
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.
Hi Carlos,
This feature works in SQL SERVER 2008R2 ?, i intent connect multiple servers with SQLCMD mode but i can’t .
Regards
Thanks
Hi Ruffo!
Yes it should, please remember what you want to leverage is the SQLCMD mode in SSMS. SQLCMD itself is an utility of SQL Server, which should be available in your SQL Server instance 🙂
Regards.
The SQLCMD function is great… My task is to extract data from 70+ SQL Server Instances all the same db,table and structure then Union All the data together in order to report off of it.
Any help would be highly appreciated.