Show TOC

Executing QueriesLocate this document in the navigation structure

Use

You can use the following utilities to execute Transact-SQL statements and stored procedures:

  • SQL Server Management Studio

    The SQL Server Management Studio is the most frequently used tool. It has a graphical user interface and is therefore easy to use. In addition, it offers quick access to help on the syntax of commands.

  • sqlcmd utility

    sqlcmd is a command line utility. The advantage of this tool is that it allows the specification of input and output files for scripts and therefore allows you to automate and schedule the execution of scripts.

Procedure

The SQL Server Management Studio

  1. Start the SQL Server Management Studio.
  2. Connect to the SQL Server instance where the database is you wish to query.
  3. Select File → New → Query with Current Connection.

    A new query window opens in the right pane.

  4. In the database field, select the database on which you want to execute a query.
  5. Enter the query. To execute the query, choose Query Execute.

    The results are displayed in a lower pane.

    Note
    • If you need help on the syntax of a statement, simply highlight the keyword and pressSHIFT F1. This takes you directly to the Transact SQL-Help that gives detailed information on the statement and illustrates it with examples.
    • A convenient feature allows you to type in several queries on the screen, but only to execute one of them. Simply highlight the statement you wish to execute before choosing the execute icon.

The sqlcmd Utility

The sqlcmd utility allows you to execute Transact-SQL statements, stored procedures and script files.

To execute a script with the sqlcmd utility, enter a command as follows in the command prompt:

sqlcmd -S<server_name>[\<instance name> -d<SAPSID> -U<login> -P<password> -i<script_name> -o<output_file>

For more information, see the topic sqlcmd utility in the SQL Server Books Online.