Click here to Skip to main content
Licence CPOL
First Posted 6 Sep 2007
Views 23,335
Bookmarked 23 times

SQL SERVER - 2005 - Introduction and Explanation to sqlcmd

By | 6 Sep 2007 | Article
SQL SERVER - 2005 - Introduction and Explanation to sqlcmd
 
Part of The SQL Zone sponsored by
See Also

Introduction

I decided to write this article to respond to a request of one usergroup, which requested that they would like to learn sqlcmd 101.

SQL Server 2005 has introduced a new utility sqlcmd to run ad hoc Transact-SQL statements and scripts from command prompt. T-SQL commands are entered in command prompt window and the result is displayed in the same window, unless the result set is sent to the output files. sqlcmd can execute single T-SQL statement as well as batch file. sqlcmd utility can connect to earlier versions of SQL Server as well. The sqlcmd utility uses the OLE DB provider to execute T-SQL commands, whereas SQL ServerManagement Studio uses .NET sqlclient to execute sqlcmd script, this can lead to different results in certain cases. (If you have an example of this, please let me know. I will post it here.)

sqlcmd is an enhanced version of the isql and osql and it provides way more functionality than the other two options. In other words, sqlcmd is a better replacement of isql (which will be deprecated eventually) and osql (not included in SQL Server 2005 RTM). sqlcmd can work two modes:

  1. BATCH and
  2. interactive

Let us go over a simple example of sqlcmd.

Step 1: Go to Start >> Run >> Type "cmd" and press enter:

Step 2: Type in command "sqlcmd" and press enter:

Step 3: Type the following "USE AdventureWorks" command to switch database context to Adventureworks. Type "GO" after the batch to change the code. It will display the success message as "Changed database context to AdventureWorks".

Step 4: Now run any same query. Refer to the following image to see the query and its result.

Step 5: We will get a similar result if the same query is run in Query Editor in SSMS.

Step 6: Type "exit" at any point if you do not want to continue working with sqlcmd.

The use of sqlcmd syntax is very easy, however, this command can perform many powerful tasks. We will see that in future articles.

Reference: Pinal Dave (http://www.SQLAuthority.com), BOL.

History

  • 6th September, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

pinaldave

Founder
http://blog.SQLAuthority.com
India India

Member

Follow on Twitter Follow on Twitter
Pinal Dave is a Microsoft Technology Evangelist (Database and BI). He has written over 2200 articles on the subject on his blog at http://blog.sqlauthority.com. Along with 8+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). He is co-author of two SQL Server books - SQL Server Programming, SQL Wait Stats and SQL Server Interview Questions and Answers. Prior to joining Microsoft he was awarded Microsoft MVP award for three continuous years for his contribution in community.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFinally... Pinmemberjoebeam10:44 6 Sep '07  
GeneralRe: Finally... Pinmemberpinaldave11:15 6 Sep '07  
GeneralRe: Finally... [modified] Pinmemberjoebeam5:55 7 Sep '07  
GeneralRe: Finally... Pinmembervjedlicka21:52 6 Sep '07  
GeneralRe: Finally... Pinmemberjoebeam6:08 7 Sep '07  
GeneralRe: Finally... Pinmemberpinaldave6:42 7 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 6 Sep 2007
Article Copyright 2007 by pinaldave
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid