SQL SERVER – Interesting Observation – Using sqlcmd From SSMS Query Editor

A day before I wrote article SQL SERVER – sqlcmd vs osql – Basic Comparison. Today while I was displaying how sqlcmd can be used instead of osql to one of my companies team leader, I found another neat feature of SSMS Query Editor. sqlcmd can be used from Query Editor but it has to be enabled first.

Following image display how sqlcmd can be enabled in Query Editor. Go to SSMS Menu >> Query >> (click on ) SQLCMD Mode.

SQL SERVER - Interesting Observation - Using sqlcmd From SSMS Query Editor sqlcmd_ex

Now on Query Editor will support sqlcmd mode. Let us see following example where script contains operating system commands as well SQL commands.

SELECT @@Version ServerVersion
!! Dir "C:\Documents and Settings"
!! Date /T
SELECT GETDATE() CurrentTime

Once above command has ran following output is displayed in Result Window.

SQL SERVER - Interesting Observation - Using sqlcmd From SSMS Query Editor sqlcmd_ex1

Interesting Observation:
What really is interesting is that out of complete batch all the operating system commands runs first and those are followed by SQL commands. The order of SQL and operating system does not matter in resultset.

I want to throw my above observation to all of you and want to get your feedback about that. Have you faced this situation or scenario? What is the solution some we want to execute SQL and OS commands in order?

Watch the video on the same subject:

Reference : Pinal Dave (https://darkslategrey-bat-805937.hostingersite.com)

SQL Scripts, SQL Server, SQL Utility, sqlcmd
Previous Post
SQL SERVER – Restore Database Backup using SQL Script (T-SQL)
Next Post
SQL SERVER – Delete Duplicate Rows

Related Posts

Leave a Reply