SQL Server Management Studio Transact SQL Query

Use the Transact-SQL Query Editor to create and run Transact-SQL scripts.


  • Type scripts in the query window.
  • To execute scripts press F5, or click Execute on the toolbar, or on the Query menu, click Execute. If a portion of the code is selected, only that portion is executed. If no code is selected, the entire content of the Query Editor is executed.
  • For help on Transact-SQL syntax, select a keyword in Query Editor, and then click F1.
  • For dynamic help with Transact-SQL syntax, on the Help menu, click Dynamic Help, to open the Dynamic Help component. With Dynamic Help, help topics appear in the Dynamic Help window when keywords are typed in Query Editor.

SQL Query Toolbar

When Query Editor is open, the SQL Query toolbar appears with the following buttons.

  • Connect
    Opens the Connect to Server dialog box, to establish a connection to a server.
  • Disconnect
    Disconnects the current Query Editor from the server.
  • Change Connection
    Opens the Connect to Server dialog box, to establish a connection to a different server.
  • New Query with Current Connection
    Opens a new Query Editor window, using the connection information from the current Query Editor window.
  • Available Databases
    Change the connection to a different database on the same server.
  • Execute
    Execute the selected code, or if no code is selected, executes the entire code in the Query Editor.
  • Parse
    Check the syntax of the selected code. If no code is selected, checks the syntax of the entire Query Editor window.
  • Cancel Executing Query
    Sends a cancellation request to the server. Some queries cannot be canceled immediately, but must wait for a suitable cancellation condition. When canceled, delays may occur while transactions are rolled back.
  • Display Estimated Execution Plan
    Requests a query execution plan from the query processor without actually executing the query, and displays the plan in the Execution plan window. This plan uses index statistics as an estimate of the number of rows expected to be returned during each portion of the query execution. The actual query plan used can be different from the estimated execution plan, if the number of rows returned is significantly different from the estimate, and the query processor changes the plan to be more efficient.
  • Analyze Query in Database Engine Tuning Advisor
    Opens the Database Engine Tuning Advisor, and provides the selected syntax as the workload for analysis.
  • Design Query in Editor
    Initiates the Query Designer tool, prompting for the tables of the current query.
  • Include Actual Execution Plan
    Executes the query, returns the query results, and the execution plan that was used for the query as a graphical query plan in the Execution plan window.
  • Include Client Statistics
    Includes a Client Statistics window with statistics about the query and about the network packets, and the elapsed time of the query.
  • SQLCMD Mode
    Executes the selected syntax using SQLCMD.


    You can execute operating system commands with SQLCMD Mode. Take care not to execute statements that prompt for a response. It is not possible to interact with the connection to respond to the request, and the query will continue to execute until canceled. For more information, see Editing SQLCMD Scripts with Query Editor.

  • Results to Text
    Returns the query results as text in the Results window.
  • Results to Grid
    Returns the query results as one or more grids in the Results window.
  • Results to File
    When the query executes, the Save Results dialog box opens. Type the File name and then click Save, to save the query results as a Report file with the .rpt extension. For advanced options, click the down-arrow on the Save button, and then click Save with Encoding.
  • Query Options
    Opens the Query Options dialog box, to configure the default options for query execution and for query results.

See Also

Other Resources

Writing, Analyzing, and Editing Scripts with SQL Server Management Studio
Editing Scripts and Files in SQL Server Management Studio
SQL Server Management Studio Keyboard Shortcuts
Customizing Menus and Shortcut Keys
Color Coding in Code Editors
Migrating from Query Analyzer to SQL Server Management Studio
Using the sqlcmd Utility

Help and Information

Getting SQL Server 2005 Assistance