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.
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.
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://blog.sqlauthority.com)