Partilhar via


Connecting with Query Editor

SQL Server Management Studio permits you to write or edit code while disconnected from the server. This can be useful when the server is not available or when you want to conserve scarce server or network resources. You can also change the connection of Query Editor to a new instance of SQL Server without opening a new Query Editor window or retyping your code.

Coding Offline

To write code offline and then connect to different servers

  1. On the Management Studio toolbar, click Database Engine Query to open the Query Editor.

  2. In the Connect to Database Engine dialog box, click Cancel. The Query Editor opens, and the title bar for the Query Editor indicates that you are not connected to an instance of SQL Server.

  3. In the code pane, type the following Transact-SQL statements:

    SELECT * FROM Production.Product;
    GO
    

At this point you can connect to an instance of SQL Server by clicking Connect, Execute, Parse, or Display Estimated Execution Plan, all of which are available from either the Query menu, the Query Editor toolbar, or from the shortcut menu when you right-click in the Query Editor window. For this practice, we'll use the toolbar.

  1. On the toolbar, click the Execute button to open the Connect to Database Engine dialog box.
  2. In the Server name text box, type your server name, and then click Options.
  3. On the Connection Properties tab, in the Connect to database list, browse the server to select AdventureWorks, and then click Connect.
  4. To open another Query Editor window with the same connection, on the toolbar click New Query.
  5. To change connections, right-click in the Query Editor window, point to Connection, and then click Change Connection.
  6. In the Connect to SQL Server dialog box, select another instance of SQL Server if available, and then click Connect.

This new feature of Query Editor enables you to easily run the same code on several servers. This may be useful for maintenance actions involving similar servers.

Next Task in Lesson

Adding Indentation