Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Nakul Vachhrajani is a Technical Specialist & Systems development professional with iGATE. He holds a MCTS (SQL Server 2008: Implementation & Maintenance)
Browse by Tags · View All
#SQLServer 233
SQL Server 232
Administration 199
DBA 188
Tips 177
Development 177
T-SQL 172
#TSQL 170
Guidance 114
Tools and Utilities 112

Archive · View All
April 2011 14
March 2012 11
December 2011 11
March 2011 11
December 2012 10
October 2011 10
January 2011 10
January 2013 9
November 2012 9
October 2012 9

SQLCMD mode in SSMS – Final part in the series “Underappreciated features of Microsoft SQL Server”

May 12 2011 12:00AM by Nakul Vachhrajani   

Today’s post is part of the series that I am running under the title "Under Appreciated Features of SQL Server". The series has been inspired by Andy Warren's editorial on SQLServerCentral.com.

The pre-requisite for today’s session is a basic knowledge of connecting to a SQL Server using the command line.

SQLCMD Utility

Batch files and command line utilities are perhaps some of the most underappreciated features of any IT establishment. When connecting to a database, we had the “OSQL” utility in the days of SQL Server 2000.

When SQL Server 2005 came about, it introduced the SQLCMD utility. This utility is a replacement of OSQL, and has a large number of advantages over the same. To list a few:

  1. Parameterized variable support
  2. Use SQLCMD to execute both T-SQL and DOS-commands (Refer Pinal’s post here)
  3. Improved Performance & security
  4. Support for DAC
  5. Multi-server connection

There may be more features of SQLCMD, which you are more than welcome to mention in the discussion associated with this post.

SQLCMD rules

There are some basic rules for any SQLCMD query. They can be summarized as under:

  1. SQLCMD commands must be the first statement on a line
  2. Only one SQLCMD command permitted on each line
  3. SQLCMD commands can be preceded by white spaces or comments
  4. Before the start of each SQLCMD command, use a colon (“:”). Only exception to this rule are “exit” and “!!” commands
  5. Single-line comment characters are two hyphens (--) and must appear at the beginning of a line

SQLCMD and SSMS

What many people don’t know is that the SQLCMD mode is also accessible from the SQL Server Management Studio. This gives you as a developer, the comfort of writing and executing SQLCMD files using the familiar environment of the SQL Server Management Studio.

Enabling SQLCMD mode in the SSMS

  1. Launch the SQL server Management Studio
  2. Click on “New Query” to open a new instance of the T-SQL Query editor
  3. Go to Query –> “SQLCMD”

image

Writing a simple query in the query editor

Taking an example from the Books On Line (http://msdn.microsoft.com/en-us/library/aa833281.aspx), write the following code in the T-SQL editor:

!!mkdir c:\TempSqlcmdOutput
:out c:\TempSqlCmdOutput\testoutput.txt
select @@VERSION as 'Server Version'
!!dir
GO
select @@SERVERNAME as 'Server Name'
GO 4

Now, click “Execute” or hit “F5” to execute the query. Navigate out to the directory - “C:\TempSqlCmdOutput”, and open the “testoutput.txt” file. You can see that the combined output of the DOS-commands and the T-SQL Query is available there.

image

Execution technology

When run from the command-line SQLCMD uses the OLE DB provider, whereas when run from SSMS, it uses the Microsoft .NET framework sqlclient.

Some limitations of the SQLCMD mode in SSMS

Because the SSMS cannot display interactive prompts, such queries cannot be executed from within the SSMS, and must be used from the command line.

Conclusion

We can now see how the most modern SSMS interface can help us write efficient command line SQLCMD queries. I personally use the SSMS to edit production level SQLCMD queries for our product and hence I urge you all to experiment with the SQLCMD mode and experience the raw power that it has to offer.

This post concludes my series on the "Under Appreciated Features of SQL Server". It has been great learning new stuff about SQL Server throughout writing of this series. I will be sharing a few of the nuggets that I have accumulated in the time to come. I hope that you, my kind readers will enjoy my future posts as well.

Until we meet next time,

Be courteous. Drive responsibly.

Tags: Development, Administration, T-SQL, Performance Tuning, Tips, SQL Server, #SQLServer, #TSQL, Best Practices, Guidance, DBA, SSMS, Tools and Utilities, Series,


Nakul Vachhrajani
4 · 33% · 10575
2
 
0
Lifesaver
 
 
0
Learned
 
0
Incorrect



Submit

4  Comments  

  • Wow !

    I wished i would have known this initially when in my last project assignment we need to exchange results of sub-queries with client in excel sheets as requirement analysis. I did got the results in excel sheet...just faced a little challenge in it. The results were not well formatted in the sheet which discourage comfortable analysis of the result.

    Can we get a proper formatted result in excel with some alteration in the way you suggested above?

    commented on Jun 9 2011 2:19AM
    abhimanyu
    226 · 1% · 197
  • Sure...you can use BCP to generate a comma-separated output, which would very easily be transferred into the excel format of your choice. You can see this in action in my post here.

    commented on Jun 9 2011 3:26AM
    Nakul Vachhrajani
    4 · 33% · 10575
  • HI nakul,

    I am very new to sqlcmd , just am trying to connect my server BUT i could't able.

    If you send me the steps it would be great help.

    commented on Jan 10 2013 1:48AM
    Bala Krishna
    85 · 2% · 642
  • Hello, "Balakrishna141"!

    You can refer the SQLCMD Tutorial on Books On Line here: http://msdn.microsoft.com/en-us/library/ms170207(v=sql.105).aspx

    commented on Jan 10 2013 12:46PM
    Nakul Vachhrajani
    4 · 33% · 10575

Your Comment


Sign Up or Login to post a comment.

"SQLCMD mode in SSMS – Final part in the series “Underappreciated features of Microsoft SQL Server”" rated 5 out of 5 by 2 readers
SQLCMD mode in SSMS – Final part in the series “Underappreciated features of Microsoft SQL Server” , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]