Technical FAQs

Ask a Question

SQLCMD - Running SQL Queries from a Command Prompt

Issue
Running SQL statements to query data.

Product Line
Microsoft SQL Server

Environment
Command Prompt (DOS Window)

Cause
In some cases there could be no access to SQL Server Management Studio.

Resolution
*Warning: Irreparable database damage can occur. This procedure should only be performed by users familiar with SQL Server and proficient in writing SQL queries . Databases should be backed up prior to running any SQL statements.*

The sqlcmd utility lets users enter Transact-SQL statements, system procedures, and script files at the command prompt.

SYNTAX
sqlcmd <command-line option>

Syntax and command line options can be found at sqlcmd utility website.

1.  An example of how to connect to a SQL Server instance and run a query against a database:

          

          -S parameter should be followed by the instance name
          -E uses a trusted connection
          GO signals the execution of the SQL statement.

2.  This example specifies the user and password when a trusted connection cannot be made:

          

          -U is the user login
          -P is the password


3.  The QUIT control command causes sqlcmd to exit:

          

 
Was this helpful?
What can we do to improve the information ?