Share via


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, 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 a query in the Transact-SQL editor

  1. Open the query in the Transact-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 Transact-SQL Editor, and click Execute SQL.

    To execute your query using a keyboard shortcut, press CTRL+SHIFT+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 Transact-SQL editor. For more 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 Transact-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 (CTRL+SHIFT+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 or the Results tab (depending if you have selected Results To Grid or Results To Text, respectively).

For more information about configuring the appropriate permissions on an instance of SQL Server, see Lesson 2: Configuring Permissions on Database Objects.

See Also

Tasks

How to: Connect to a Database in the Transact-SQL Editor

How to: Disconnect from Databases

How to: Connect to Different Databases in the Same Editor Session

Walkthrough: Create and Execute a Simple Transact-SQL Script

Concepts

Editing Database Scripts and Objects with the Transact-SQL Editor