How to: Execute a Query

After you write a script or query, you will typically want to execute it and examine the results. Before you execute your query, you should verify that you are connected to the appropriate database and server. If you attempt to execute your query but are not connected, you will be prompted for a connection.

To execute your query in the Transact-SQL editor

  1. Open the query in the Transact-SQL (T-SQL) editor. For information about how to start the editor, see How to: Start the Transact-SQL Editor.

  2. On the Data menu, point to T-SQL Editor, and click Execute SQL.

    To execute your query using a keyboard shortcut, press F5 (or CTRL+E).

    To execute your query from the toolbar, click Execute SQL.

    To execute your query from a shortcut menu, right-click in the T-SQL editor, and then click Execute SQL.

    The query executes against the database to which you are connected. The time that it takes to return results to you will vary based on the complexity of the query, the load on the database server, and the specifications of the database server.

    For information about how to cancel an executing query, see How to: Cancel a Running Query.

    For information about how to view the results of a query, see Query Results Overview.

To execute one statement out of a multi-statement script or query

  1. Open the query in the T-SQL editor. For information about how to start the editor, see How to: Start the Transact-SQL Editor.

  2. In the editor, highlight the statement that you want to execute.

  3. On the T-SQL editor toolbar, click Execute SQL.

    The statement that you highlighted executes against the database to which you are connected. The time that it takes to return results to you will vary based on a number of factors. These factors include the complexity of the query, the load on the database server, and the specifications of the database server.

    You can also execute the highlighted statement by using the keyboard shortcut (F5 or CTRL+E), the toolbar, or the shortcut menu, as noted in the previous procedure.

    For information about how to cancel an executing query, see How to: Cancel a Running Query.

    For information about how to view the results of a query, see Query Results Overview.

Security

You can run scripts and queries only if you have the appropriate permissions on the server, the database, and the database objects being accessed. For example, if you do not have permissions to run stored procedures against a database, execution of scripts that call stored procedures will fail.

If you lack sufficient permissions to execute all or part of a script, errors appear on the Messages tab of the results pane and in the Error List window.

See Also

Tasks

Walkthrough: Create and Execute a Simple Transact-SQL Script

Other Resources

Script Analysis and Execution in the Transact-SQL Editor
Managing Database Connections within the Transact-SQL Editor
Transact-SQL Editing Essentials