Skip to content

Commit 9b92c01

Browse files
authored
Merge pull request #5307 from itechedit/connect-query-sql-server
edit pass: connect-query-sql-server
2 parents 6c47f1d + 4f82832 commit 9b92c01

1 file changed

Lines changed: 73 additions & 71 deletions

File tree

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
Title: "Tutorial: Connect and Query SQL Server using SQL Server Management Studio"
3-
description: A Tutorial for connecting to SQL Server using SQL Server Management Studio and running basic T-SQL queries.
2+
Title: "Tutorial: Connect to and query a SQL Server instance by using SQL Server Management Studio"
3+
description: A tutorial for connecting to a SQL Server instance by using SQL Server Management Studio and running basic T-SQL queries.
44
keywords: SQL Server, SSMS, SQL Server Management Studio
55
author: MashaMSFT
66
ms.author: mathoma
@@ -12,63 +12,62 @@ ms.reviewer: sstein
1212
manager: craigg
1313
---
1414

15-
# Tutorial: Connect and Query SQL Server using SQL Server Management Studio
16-
This Tutorial teaches you how to use SQL Server Management Studio (SSMS) to connect to your SQL Server instance, and run some basic Transact-SQL (T-SQL) commands. This article demonstrates how to do the following:
17-
18-
> [!div class="checklist"]
19-
> * [Connect to a SQL Server](#connect-to-a-sql-server)
20-
> * [Create a new database (**TutorialDB**)](#create-a-database)
21-
> * [Create a table (**Customers**) in your new database](#create-a-table)
22-
> * [Insert rows into your new **Customers** table](#insert-rows)
23-
> * [Query the **Customers** table and view the results](#view-query-results)
24-
> * [Use the query window table to verify your connection properties](#verify-your-query-window-connection-properties)
25-
> * [Change which server your query window is connected to](#change-server-connection-within-query-window)
15+
# Tutorial: Connect to and query a SQL Server instance by using SQL Server Management Studio
16+
This tutorial teaches you how to use SQL Server Management Studio (SSMS) to connect to your SQL Server instance and run some basic Transact-SQL (T-SQL) commands. The article demonstrates how to do the following:
2617

18+
> [!div class="checklist"]
19+
> * Connect to a SQL Server instance
20+
> * Create a database ("TutorialDB")
21+
> * Create a table ("Customers") in your new database
22+
> * Insert rows into your new table
23+
> * Query the new table and view the results
24+
> * Use the query window table to verify your connection properties
25+
> * Change the server that your query window is connected to
2726
2827
## Prerequisites
29-
To complete this Tutorial, you need SQL Server Management Studio and access to a SQL Server.
28+
To complete this tutorial, you need SQL Server Management Studio and access to a SQL Server instance.
3029

3130
- Install [SQL Server Management Studio](https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms).
3231

33-
If you don't have access to a SQL Server, select your platform from the following links (make sure you remember your SQL Login and Password if you choose SQL Authentication!):
34-
- [Windows - Download SQL Server 2017 Developer Edition](https://www.microsoft.com/en-us/sql-server/sql-server-downloads)
35-
- [macOS - Download SQL Server 2017 on Docker](https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker)
32+
If you don't have access to a SQL Server instance, select your platform from the following links. If you choose SQL Authentication, use your SQL Server login credentials.
33+
- **Windows**: [Download SQL Server 2017 Developer Edition](https://www.microsoft.com/en-us/sql-server/sql-server-downloads).
34+
- **macOS**: [Download SQL Server 2017 on Docker](https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker).
3635

3736

38-
## Connect to a SQL Server
37+
## Connect to a SQL Server instance
3938

40-
1. Start SQL Server Management Studio (SSMS).
41-
1. The first time you run SSMS the **Connect to Server** dialog box opens.
42-
- If the **Connect to Server** dialog doesn't open, it can be opened manually in **Object Explorer** > **Connect** (or icon next to it) > **Database Engine**.
39+
1. Start SQL Server Management Studio.
40+
The first time you run SSMS, the **Connect to Server** window opens. If it doesn't open, you can open it manually by selecting **Object Explorer** > **Connect** > **Database Engine**.
4341

44-
![Connect in Object Explorer](media/connect-query-sql-server/connectobjexp.png)
42+
![The Connect link in Object Explorer](media/connect-query-sql-server/connectobjexp.png)
4543

46-
1. In the **Connect to Server** dialog box, fill out your connection options:
44+
2. In the **Connect to Server** window, do the following:
4745

48-
- **Server type**: Database Engine (typically selected by default).
49-
- **Server Name**: This article uses the instance name SQL2016ST on the hostname NODE5 (NODE5\SQL2016ST), but you'll need to type in your own server name here. If you're not sure how to determine your SQL Server name, you can find more information [here](ssms-tricks.md#determine-sql-server-name).
50-
- **Authentication**: Windows Authentication (this article uses Windows Authentication, but SQL Login is supported and will prompt you for a username and password if selected). More information on authentication types can be found [here](https://docs.microsoft.com/en-us/sql/ssms/f1-help/connect-to-server-database-engine).
46+
- For **Server type**, select **Database Engine** (usually the default option).
47+
- For **Server name**, enter the name of your SQL Server instance. (This article uses the instance name SQL2016ST on the hostname NODE5 [NODE5\SQL2016ST].) If you're unsure how to determine your SQL Server instance name, see [Additional tips and tricks for using SSMS](ssms-tricks.md#determine-sql-server-name).
5148

52-
![![Connection](media/connect-query-sql-server/connection.png)](media/connect-query-sql-server/connection2.png)
49+
!["Server name" field with example instance name](media/connect-query-sql-server/connection.png)
5350

54-
You can also modify additional connection options (such as the database you're connecting to, the connection timeout value, and the network protocol) by clicking the **Options** button. For the purpose of this article, everything was left at the default values.
51+
!["Server name" field with option of using SQL Server instance](media/connect-query-sql-server/connection2.png)
5552

53+
- For **Authentication**, select **Windows Authentication**. This article uses Windows Authentication, but SQL Server login is also supported. If you select **SQL Login**, you will be prompted for a username and password. For more information about authentication types, see [Connect to server (database engine)](https://docs.microsoft.com/en-us/sql/ssms/f1-help/connect-to-server-database-engine).
5654

57-
1. Once the fields have been filled out, click on **Connect**.
55+
You can also modify additional connection options by selecting **Options**. Examples of connection options are the database you're connecting to, the connection timeout value, and the network protocol. This article uses the default values for all the options.
5856

59-
1. You can verify that your connection succeeded to your SQL Server by exploring the objects in **Object Explorer**:
57+
3. After you've completed all the fields, select **Connect**.
6058

61-
![Successful Connection](media/connect-query-sql-server/successfulconnection.png)
59+
4. Verify that the connection to your SQL Server instance is successful by exploring the objects in Object Explorer, as shown here:
6260

61+
![Successful connection](media/connect-query-sql-server/successfulconnection.png)
6362

6463
## Create a database
65-
The following steps create a database named TutorialDB.
64+
Create a database named TutorialDB by doing the following:
6665

67-
1. Right-click your server in **Object Explorer** and select **New Query**:
66+
1. Right-click your server instance in Object Explorer, and then select **New Query**:
6867

69-
![New Query](media/connect-query-sql-server/newquery.png)
68+
![The New Query link](media/connect-query-sql-server/newquery.png)
7069

71-
1. Paste the following T-SQL code snippet into the query window:
70+
2. Into the query window, paste the following T-SQL code snippet:
7271
```sql
7372
USE master
7473
GO
@@ -80,23 +79,22 @@ The following steps create a database named TutorialDB.
8079
CREATE DATABASE [TutorialDB]
8180
GO
8281
```
83-
2. To execute the query, click on **Execute** (or press F5 on your keyboard).
82+
2. To execute the query, select **Execute** (or select F5 on your keyboard).
8483

85-
![Execute Query](media/connect-query-sql-server/execute.png)
84+
![The Execute command](media/connect-query-sql-server/execute.png)
8685

87-
88-
After the query completes, the new **TutorialDB** appears in the list of databases within **Object Explorer**. If you don’t see it, right-click the Databases node and select **Refresh**.
86+
After the query is complete, the new TutorialDB database appears in the list of databases in Object Explorer. If it isn't displayed, right-click the **Databases** node, and then select **Refresh**.
8987

9088

91-
## Create a Table
92-
The following steps will now create a table in the newly created **TutorialDB** database. However, the query editor is still in the context of the *master* database, and you want to create a table in the *TutorialDB* database.
89+
## Create a table in the new database
90+
In this section, you create a table in the newly created TutorialDB database. Because the query editor is still in the context of the *master* database, switch the connection context to the *TutorialDB* database by doing the following:
9391

94-
1. Change the connection context of your query from the master database to **TutorialDB** by selecting the database you want from the database drop-down.
92+
1. In the database drop-down list, select the database that you want, as shown here:
9593

9694
![Change database](media/connect-query-sql-server/changedb.png)
9795

98-
1. Paste the following T-SQL code snippet into the query window, highlight it, and click **Execute** (or press F5 on your keyboard):
99-
- You can either replace the existing text in the query window or append it to the end. If you want to execute everything in the query window, click **Execute**. If you want to execute a portion of the text, highlight that portion, and then click **Execute**.
96+
2. Paste the following T-SQL code snippet into the query window, select it, and then select **Execute** (or select F5 on your keyboard).
97+
You can either replace the existing text in the query window or append it to the end. To execute everything in the query window, select **Execute**. To execute a portion of the text, highlight that portion, and then select **Execute**.
10098

10199
```sql
102100
-- Create a new table called 'Customers' in schema 'dbo'
@@ -114,12 +112,11 @@ The following steps will now create a table in the newly created **TutorialDB**
114112
);
115113
GO
116114
```
117-
After the query completes, the new **Customers** table appears in the list of tables in **Object Explorer**. If the table is not visible, right-click the **TutorialDB > Tables** node in **Object Explorer** and select **Refresh**.
118115

119-
## Insert rows
120-
The following step will insert some rows into the **Customers** table that was previously created.
116+
After the query is complete, the new Customers table is displayed in the list of tables in Object Explorer. If the table is not displayed, right-click the **TutorialDB** > **Tables** node in Object Explorer, and then select **Refresh**.
121117

122-
Paste the following T-SQL code snippet into the query window and click **Execute**:
118+
## Insert rows into the new table
119+
Insert some rows into the Customers table that you created previously. To do so, paste the following T-SQL code snippet into the query window, and then select **Execute**:
123120

124121

125122
```sql
@@ -134,44 +131,49 @@ Paste the following T-SQL code snippet into the query window and click **Execute
134131
GO
135132
```
136133

137-
## View Query Results
138-
The results of a query are visible underneath the query text window. The below steps will allow you to query the **Customers** table and view the rows that were previously inserted.
134+
## Query the table and view the results
135+
The results of a query are visible below the query text window. To query the Customers table and view the rows that were previously inserted, do the following:
139136

140-
1. Paste the following T-SQL code snippet into the query window and click **Execute**:
137+
1. Paste the following T-SQL code snippet into the query window, and then select **Execute**:
141138

142139
```sql
143140
-- Select rows from table 'Customers'
144141
SELECT * FROM dbo.Customers;
145142
```
146-
1. The results of the query are displayed under the area where text was entered:
147143

148-
![Query Results](media/connect-query-sql-server/queryresults.png)
144+
The results of the query are displayed under the area where text was entered:
145+
146+
![The Results list](media/connect-query-sql-server/queryresults.png)
149147

148+
2. Modify the way results are presented by selecting one of the following options:
150149

151-
1. You can modify the way results are presented by selecting one of these options:
150+
![Three options for displaying query results](media/connect-query-sql-server/results.png)
152151

153-
![results](media/connect-query-sql-server/results.png)
152+
- The middle button displays the results in **Grid View**, which is the default option.
153+
- The first button displays the results in **Text View**, as shown in the image in the next section.
154+
- The third button lets you save the results to a file whose extension is .rpt by default.
154155

155-
- By default, the results will be in **Grid View**, which is the middle button and shows the results in a table.
156-
- The first button will display your results in **Text View**, as shown in the image in the next section.
157-
- The third button will allow you to save your results to a file, a file ending in *.rpt by default.
156+
## Verify your connection properties by using the query window table
157+
You can find information about the connection properties under the results of your query. After you run the previously mentioned query in the preceding step, review the connection properties at the bottom of the query window.
158158

159-
## Verify your query window connection properties
160-
You can find information about the connection properties under the results of your query.
161-
- After running the aforementioned query from the previous step, review the connection properties at the bottom of the query window.
162-
- You can determine which server and database you're connected to, and the user you're logged in with.
163-
- You can also see the query duration and the number of rows returned by the query executed earlier.
159+
- You can determine which server and database you're connected to, and the username that you're logged in with.
160+
- You can also view the query duration and the number of rows that are returned by the previously executed query.
161+
162+
![Connection properties](media/connect-query-sql-server/connectionproperties.png)
164163

165-
![Connection Properties](media/connect-query-sql-server/connectionproperties.png)
166-
In this image, the results are displayed in **Text View**.
164+
In the image, note that the results are displayed in **Text View**.
165+
166+
## Change the server that the query window is connected to
167+
You can change the server that your current query window is connected to by doing the following:
167168

168-
## Change server connection within Query Window
169-
You can change which server your current query window is connected to by following these steps.
170-
1. Right click within the query window > Connection > Change connection.
171-
2. This will open the **Connect to Server** dialog box again, allowing you to change which server your query is connected to.
169+
1. Right-click in the query window, and then select **Connection** > **Change connection**.
170+
The **Connect to Server** window opens again.
171+
2. Change the server that your query is connected to.
172172

173-
![Change Connection](media/connect-query-sql-server/changeconnection.png)
174-
- Note that this does not change which server your **Object Explorer** is connected to, just the current query window.
173+
![The Change Connection command](media/connect-query-sql-server/changeconnection.png)
174+
175+
> [!NOTE]
176+
> This action changes only the server that the query window is connected to, not the server that Object Explorer is connected to.
175177
176178

177179

0 commit comments

Comments
 (0)