SQLCMD Step by Step

Format
mla apa chicago
Your Citation
Chapple, Mike. "SQLCMD Step by Step." ThoughtCo, Mar. 19, 2017, thoughtco.com/sqlcmd-step-by-step-1019881. Chapple, Mike. (2017, March 19). SQLCMD Step by Step. Retrieved from https://www.thoughtco.com/sqlcmd-step-by-step-1019881 Chapple, Mike. "SQLCMD Step by Step." ThoughtCo. https://www.thoughtco.com/sqlcmd-step-by-step-1019881 (accessed October 24, 2017).
01
of 06

Getting Familiar With SQLCMD

Mike Chapple

Microsoft SQL Server provides users with a variety of rich graphical user interfaces for retrieving and manipulating data and configuring SQL Server databases. However, sometimes it’s just easier to work from the old-fashioned command line. Whether you’re looking for a quick-and-dirty way to execute a SQL query or wish to include SQL statements in a Windows script file, SQLCMD allows you to meet your goal.

This article assumes you already have Microsoft's AdventureWorks Sample Database installed.

02
of 06

Opening a Command Prompt

Mike Chapple

In order to run SQLCMD, you must first open a Windows command line utility. In Windows XP, click Start > Run and then type CMD in the text box before clicking OK. In Windows Vista, click the Windows button, type CMD into the Search box and press Enter.

You should see a Windows command prompt.

03
of 06

Connecting to the Database

Mike Chapple

Once you have a command prompt open, use the SQLCMD utility to connect to the database. In this example, we're connecting to the AdventureWorks2014 database, so we use the command:


sqlcmd -d AdventureWorks2014

This uses the default Windows credentials to connect to your database. You may also specify a username using the -U flag and a password using the -P flag. For example, you could connect to the database using the username "mike" and password "goirish" with the following command line:


sqlcmd -U mike -P goirish -d AdventureWorks2014
04
of 06

Entering a Query

Mike Chapple

Begin typing a SQL statement at the 1> prompt. You can use as many lines as you want for your query, pressing the Enter key after each line. SQL Server does not execute your query until explicitly instructed to do so.

In this example, we enter the following query:

 SELECT *
 FROM HumanResources.shift
 
05
of 06

Executing the Query

Mike Chapple

When you are ready to execute your query, type the command GO on a new command line within SQLCMD and press Enter. SQLCMD executes your query and displays the results on the screen.

06
of 06

Exiting SQLCMD

When you are ready to exit SQLCMD, type the command EXIT on a blank command line to return to the Windows command prompt.

Format
mla apa chicago
Your Citation
Chapple, Mike. "SQLCMD Step by Step." ThoughtCo, Mar. 19, 2017, thoughtco.com/sqlcmd-step-by-step-1019881. Chapple, Mike. (2017, March 19). SQLCMD Step by Step. Retrieved from https://www.thoughtco.com/sqlcmd-step-by-step-1019881 Chapple, Mike. "SQLCMD Step by Step." ThoughtCo. https://www.thoughtco.com/sqlcmd-step-by-step-1019881 (accessed October 24, 2017).