Skip to content

Commit e42f92e

Browse files
authored
Merge pull request #5593 from TrishaMc/scripting-ssms
edit pass: scripting-ssms
2 parents 925d7c1 + 2c0e578 commit e42f92e

File tree

1 file changed

+87
-85
lines changed

1 file changed

+87
-85
lines changed

docs/ssms/tutorials/scripting-ssms.md

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
Title: "Tutorial: Script Objects in SQL Server Management Studio"
3-
description: "A Tutorial for scripting out objects in SSMS."
2+
Title: "Tutorial: Script objects in SQL Server Management Studio"
3+
description: "A tutorial for scripting out objects in SSMS"
44
keywords: SQL Server, SSMS, SQL Server Management Studio, Scripts, Scripting
55
author: MashaMSFT
66
ms.author: mathoma
@@ -23,174 +23,176 @@ helpviewer_keywords:
2323
- "scripts [SQL Server], SQL Server Management Studio"
2424
---
2525

26-
# Tutorial: Script Objects in SQL Server Management Studio
27-
This tutorial will teach you how to generate Transact-SQL (T-SQL) scripts for various objects found within SQL Server Management Studio. In this tutorial, you will find examples of how to script the following objects:
26+
# Tutorial: Script objects in SQL Server Management Studio
27+
This tutorial teaches you to generate Transact-SQL (T-SQL) scripts for various objects found within SQL Server Management Studio (SSMS). In this tutorial, you find examples of how to script the following objects:
2828

2929
> [!div class="checklist"]
30-
> * Queries when performing actions within the GUI
31-
> * Databases in two different ways ("Script As" and "Generate Script")
30+
> * Queries, when you perform actions within the GUI
31+
> * Databases in two different ways (Script As and Generate Script)
3232
> * Tables
3333
> * Stored procedures
3434
> * Extended events
3535
36-
The summary of this tutorial is that any object in **Object Explorer** can be scripted by right-clicking it and selecting the **Script Object As** option.
36+
To script any object in **Object Explorer**, right-click it and select the **Script Object As** option. This tutorial shows you the process.
3737

3838

3939
## Prerequisites
40-
To complete this Tutorial, you need SQL Server Management Studio, access to a SQL Server, and an AdventureWorks database.
40+
To complete this tutorial, you need SQL Server Management Studio, access to a server that's running SQL Server, and an AdventureWorks database.
4141

4242
- Install [SQL Server Management Studio](https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms).
4343
- Install [SQL Server 2017 Developer Edition](https://www.microsoft.com/en-us/sql-server/sql-server-downloads).
44-
- Download [AdventureWorks2016 Sample Databases](https://github.com/Microsoft/sql-server-samples/releases). Instructions for restoring databases in SSMS can be found here: [Restoring a Database](https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms).
44+
- Download [AdventureWorks2016 sample databases](https://github.com/Microsoft/sql-server-samples/releases).
4545

46+
Instructions for restoring databases in SSMS are here: [Restore a database](https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms).
4647

47-
## Script Queries from GUI
48-
Any time you perform a task using the GUI in SSMS, you can also generate the T-SQL code associated with that task. The following examples show how to do so when taking a backup of a database, and when you shrink the transaction log. These same steps can be applied to any action that's completed via the GUI.
4948

50-
### Script T-SQL when backing up a database
51-
1. Connect to your SQL Server.
49+
## Script queries from the GUI
50+
You can generate the associated T-SQL code for a task whenever you use the GUI in SSMS to complete it. The following examples show how to do so when you back up a database and when you shrink the transaction log. These same steps can be applied to any action that's completed via the GUI.
51+
52+
### Script T-SQL when you back up a database
53+
1. Connect to a server that's running SQL Server.
5254
2. Expand the **Databases** node.
53-
3. Right-click the **Adventureworks2016** database > **Tasks** > **Back up**:
55+
3. Right-click the database **Adventureworks2016** > **Tasks** > **Back Up**:
5456

55-
![Back up Database](media/scripting-ssms/backupdb.png)
57+
![Back up a database](media/scripting-ssms/backupdb.png)
5658

57-
4. Configure the backup the way you want. For the purpose of this Tutorial, everything is left at default. However, any changes made in the window will also be reflected in the script.
58-
5. Select the option to **Script** > **Script Action to Query Window**:
59+
4. Configure the backup the way you want. For this tutorial, everything is left at default. However, any changes made in the window also reflect in the script.
60+
5. Select **Script** > **Script Action to New Query Window**:
5961

60-
![Script DB Backup](media/scripting-ssms/scriptdbbackup.PNG)
61-
6. Review the T-SQL populated in the query window:
62+
![Script database backup--script action](media/scripting-ssms/scriptdbbackup.PNG)
63+
6. Review the T-SQL populated in the query window.
6264

63-
![Script for DB Backup](media/scripting-ssms/dbbackupscript.PNG)
64-
7. Select **Execute** to execute the query to backup the database via T-SQL.
65+
![Script database backup--review T-SQL](media/scripting-ssms/dbbackupscript.PNG)
66+
7. Select **Execute** to execute the query to back up the database via T-SQL.
6567

6668

67-
### Script T-SQL when shrinking the transaction log
68-
1. Right-click the **AdventureWorks2016** database > **Tasks** > **Shrink** > **Files**:
69+
### Script T-SQL when you shrink the transaction log
70+
1. Right-click the database **AdventureWorks2016** > **Tasks** > **Shrink** > **Files**:
6971

70-
![Shrink Files](media/scripting-ssms/shrinkfiles.png)
72+
![Shrink files](media/scripting-ssms/shrinkfiles.png)
7173

72-
2. Select **Log** from the **File Type** drop down:
74+
2. Select **Log** from the **File type** drop-down list box:
7375

74-
![Shrink Transaction Log](media/scripting-ssms/shrinktlog.png)
76+
![Shrink transaction log](media/scripting-ssms/shrinktlog.png)
7577

76-
3. Select the option **Script** and **Script Action to Clipboard**:
78+
3. Select **Script** and **Script Action to Clipboard**:
7779

78-
![Script to Clipboard](media/scripting-ssms/scriptactiontoclipboard.png)
80+
![Script to clipboard](media/scripting-ssms/scriptactiontoclipboard.png)
7981

80-
4. Open a **New Query** window and paste (Right-click in the window > **Paste**):
82+
4. Open a **New Query** window and paste. (Right-click in the window. Then select **Paste**.)
8183

82-
![Paste Script](media/scripting-ssms/paste.png)
84+
![Paste script](media/scripting-ssms/paste.png)
8385
5. Select **Execute** to execute the query and shrink the transaction log.
8486

8587

86-
## Script Databases
87-
The following section teaches you how to script out the database, both using the **Script As** option and the **Generate Scripts** option. The **Script As** option will recreate the database and the configuration options for it. The **Generate Scripts** option will allow you to script both the schema and the data. In this section, you will create two new databases, *AdventureWorks2016a* will be created using the **Script As** option. *AdventureWorks2016b* will be created using the **Generate Scripts** option.
88+
## Script databases
89+
The following section teaches you to script out the database by using the **Script As** and **Generate Scripts** options. The **Script As** option re-creates the database and its configuration options. You can script both the schema and the data by using the **Generate Scripts** option. In this section, you create two new databases. You use the **Script As** option to create *AdventureWorks2016a*. You use the **Generate Scripts** option to create *AdventureWorks2016b*.
8890

8991

90-
### Script database using Script option
91-
1. Connect to your SQL Server.
92+
### Script a database by using the Script option
93+
1. Connect to a server that's running SQL Server.
9294
2. Expand the **Databases** node.
93-
3. Right-click the **AdventureWorks2016** database > **Script Database As** > **Create To** > **New Query Window**:
95+
3. Right-click the database **AdventureWorks2016** > **Script Database As** > **Create To** > **New Query Editor Window**:
9496

95-
![Script DB](media/scripting-ssms/scriptdb.png)
97+
![Script database](media/scripting-ssms/scriptdb.png)
9698

9799
4. Review the database creation query in the window:
98100

99-
![Scripted out DB](media/scripting-ssms/scriptedoutdb.png)
100-
- This option will only script out the database configuration options.
101-
5. On your keyboard, select **Ctrl + F** to open the **Find** dialog box and select the down arrow to open the **Replace** option. On the top **Find** line, type in *AdventureWorks2016* and on the bottom **Replace** line, type in *AdventureWorks2016a*.
101+
![Scripted-out database](media/scripting-ssms/scriptedoutdb.png)
102+
This option scripts out only the database configuration options.
103+
5. On your keyboard, select Ctrl+F to open the **Find** dialog box. Select the down arrow to open the **Replace** option. On the top **Find** line, type AdventureWorks2016, and on the bottom **Replace** line, type AdventureWorks2016a.
102104
6. Select **Replace All** to replace all instances of *AdventureWorks2016* with *AdventureWorks2016a*.
103105

104-
![Find and Replace](media/scripting-ssms/findandreplace.png)
106+
![Find and replace](media/scripting-ssms/findandreplace.png)
105107

106-
1. Select **Execute** to execute the query and create your new *AdventureWorks2016a* database.
108+
1. Select **Execute** to execute the query and create your new AdventureWorks2016a database.
107109

108-
### Script database using Generate Scripts option
109-
1. Connect to your SQL Server.
110+
### Script a database by using the Generate Scripts option
111+
1. Connect to a server that's running SQL Server.
110112
2. Expand the **Databases** node.
111-
3. Right-click the **AdventureWorks2016** database > **Tasks** > **Generate Scripts**:
113+
3. Right-click **AdventureWorks2016** > **Tasks** > **Generate Scripts**:
112114

113-
![Generate Scripts for DB](media/scripting-ssms/generatescriptsfordb.png)
115+
![Generate scripts for databases](media/scripting-ssms/generatescriptsfordb.png)
114116

115-
4. The **Introduction** page opens, select **Next** to open **Chose Objects** page. You have the option to select the entire database or specific objects in the database. Select the option to **Script entire database and all database objects**
117+
4. The **Introduction** page opens. Select **Next** to open the **Chose Objects** page. You can select the entire database or specific objects in the database. Select **Script entire database and all database objects**.
116118

117-
![Generate Scripts for Objects](media/scripting-ssms/scriptobjects.png)
119+
![Generate scripts for objects](media/scripting-ssms/scriptobjects.png)
118120

119-
5. Select **Next** to open the **Set Scripting Options** page, which is where you can configure where to save the script as well as some additional advanced options.
121+
5. Select **Next** to open the **Set Scripting Options** page. Here you can configure where to save the script and some additional advanced options.
120122

121-
a. Select the option to **Save to new query Window**.
123+
a. Select **Save to new query window**.
122124

123-
b. Select **Advanced** and ensure these options are set:
125+
b. Select **Advanced** and make sure these options are set:
124126

125-
- **Script Statistics** set to *Script Statistics*
126-
- **Types of data to script** set to *Schema only*
127-
- **Script Indexes** set to *true*
127+
- **Script Statistics** set to *Script Statistics*.
128+
- **Types of data to script** set to *Schema only*.
129+
- **Script Indexes** set to *True*.
128130

129-
![Script Objects](media/scripting-ssms/advancedscripts.png)
131+
![Script objects](media/scripting-ssms/advancedscripts.png)
130132

131133
> [!NOTE]
132-
> You have the ability to script the data for the database when you select *Schema and data* for the **Types of data to script** option. However, this is not ideal with large databases as it can take more memory than SSMS is able to allocate. This is okay for small databases, but if you want to move data for a larger database, you should use the [Import and Export Wizard](https://docs.microsoft.com/en-us/sql/integration-services/import-export-data/import-and-export-data-with-the-sql-server-import-and-export-wizard).
133-
134+
> You can script the data for the database when you select *Schema and data* for the **Types of data to script** option. However, this isn't ideal with large databases. It can take more memory than SSMS can allocate. This limitation is okay for small databases. If you want to move data for a larger database, use the [Import and Export Wizard](https://docs.microsoft.com/en-us/sql/integration-services/import-export-data/import-and-export-data-with-the-sql-server-import-and-export-wizard).
134135
135136

136-
1. Select **OK** and then select **Next**.
137-
2. Select **Next** on the **Summary** and then select **Next** again to generate the script to a **New Query** Window.
138-
3. On your keyboard, select **Ctrl + F** to open the **Find** dialog box and select the down arrow to open the **Replace** option. On the top **Find** line, type in *AdventureWorks2016* and on the bottom **Replace** line, type in *AdventureWorks2016b*.
139-
a. Select **Replace All** to replace all instances of *AdventureWorks2016* with *AdventureWorks2016b*.
137+
1. Select **OK**, and then select **Next**.
138+
2. Select **Next** on the **Summary**. Then select **Next** again to generate the script in a **New Query** window.
139+
3. On your keyboard, open the **Find** dialog box (Ctrl+F). Select the down arrow to open the **Replace** option. On the top **Find** line, enter *AdventureWorks2016*. On the bottom **Replace** line, enter *AdventureWorks2016b*.
140+
4. Select **Replace All** to replace all instances of *AdventureWorks2016* with *AdventureWorks2016b*.
140141

141142
![AdventureWorks2016b](media/scripting-ssms/adventureworks2016b.png)
142-
7. Select **Execute** to execute the query and create your new *AdventureWorks2016b* database.
143-
144-
## Script Tables
145-
This section covers how to script out tables from your database. Using this option, you can either create the table, or drop and create the table. You can also use this option to script the T-SQL associated with modifying the table, such as to insert into it, or update to it. In this section, you will drop a table, and then recreate it.
143+
7. Select **Execute** to execute the query and create your new AdventureWorks2016b database.
146144

147-
1. Connect to your SQL Server.
145+
## Script tables
146+
This section covers how to script out tables from your database. Use this option to either create the table or drop and create the table. You can also use this option to script the T-SQL associated with modifying the table. An example is to insert into it or update to it. In this section, you drop a table and then re-create it.
147+
148+
1. Connect to a server that's running SQL Server.
148149
2. Expand your **Databases** node.
149-
3. Expand your **AdventureWorks** database node.
150+
3. Expand your **AdventureWorks2016** database node.
150151
4. Expand your **Tables** node.
151-
5. Right-click the **dbo.ErrorLog** > **Script Table as** > **Drop and Create To** > **New Query Editor Window**:
152+
5. Right-click **dbo.ErrorLog** > **Script Table as** > **DROP And CREATE To** > **New Query Editor Window**:
152153

153-
![Script Table](media/scripting-ssms/scripttable.png)
154+
![Script table](media/scripting-ssms/scripttable.png)
154155

155-
6. Select **Execute** to execute the query - this will drop the *Errorlog* table and recreate it.
156+
6. Select **Execute** to execute the query. This action drops the *Errorlog* table and re-creates it.
156157

157158
>[!NOTE]
158-
> The *Errorlog* table is empty by default in the AdventureWorks2016 database, so you're not losing any data by dropping the table. However, following these steps on a table with data will cause data loss.
159+
> The *Errorlog* table is empty by default in the AdventureWorks2016 database. So you're not losing any data by dropping the table. However, following these steps on a table with data causes data loss.
159160
160-
## Script Stored Procedures
161-
In this section, you will learn how to drop and create a stored procedure.
161+
## Script stored procedures
162+
In this section, you'll learn how to drop and create a stored procedure.
162163

163-
1. Connect to your SQL Server.
164+
1. Connect to a server that's running SQL Server.
164165
2. Expand your **Databases** node.
165166
3. Expand your **Programmability** node.
166167
4. Expand your **Stored Procedure** node.
167-
5. Right-click the stored procedure **dbo.uspGetBillOfMaterials**> **Script Stored Procedure As** > **Drop and Create to** > **New Query Window**:
168+
5. Right-click the stored procedure **dbo.uspGetBillOfMaterials** > **Script Stored Procedure As** > **DROP and CREATE To** > **New Query Editor Window**:
168169

169-
![Script Stored Procedures](media/scripting-ssms/scriptstoredprocedure.PNG)
170+
![Script stored procedures](media/scripting-ssms/scriptstoredprocedure.PNG)
170171

171-
## Script Extended Events
172-
This section covers how to script out [extended events](https://docs.microsoft.com/en-us/sql/relational-databases/extended-events/extended-events).
172+
## Script extended events
173+
This section covers how to script out [extended events](https://docs.microsoft.com/en-us/sql/relational-databases/extended-events/extended-events).
173174

174-
1. Connect to your SQL Server.
175+
1. Connect to a server that's running SQL Server.
175176
2. Expand your **Management** node.
176177
3. Expand your **Extended Events** node.
177178
4. Expand your **Sessions** node.
178179
5. Right-click the extended session you're interested in > **Script Session As** > **New Query Editor Window**:
179180

180-
![Script xEvents](media/scripting-ssms/scriptxevents.png)
181-
6. In the **New Query Window**, modify the new name of the session from *system_health* to *system_health2* and select **Execute** to execute the query.
181+
![Extended New Query Editor Window session](media/scripting-ssms/scriptxevents.png)
182+
183+
6. In the **New Query Editor Window**, modify the new name of the session from *system_health* to *system_health2*. Select **Execute** to execute the query.
182184

183-
a. Right-click **Sessions** in **Object Explorer** and select **Refresh** to see your new Extended Event Session. The green icon next to the session indicates the session is running whereas the red icon indicates the session is stopped.
185+
7. Right-click **Sessions** in **Object Explorer**. Select **Refresh** to see your new extended event session. The green icon next to the session indicates the session is running. The red icon indicates the session is stopped.
184186

185-
![New xEvent](media/scripting-ssms/newxevent.png)
187+
![New extended event session](media/scripting-ssms/newxevent.png)
186188

187189
>[!NOTE]
188-
> You can start the session by right-clicking it and selecting **Start**. However, since this is a copy of the already-running *system_health* session, this step can be skipped. You can delete the copy of the extended event session by right-clicking it and selecting **Delete**.
190+
> You can start the session by right-clicking it and selecting **Start**. However, this is a copy of the already running **system_health** session, so you can skip this step. You can delete the copy of the extended event session: right-click it and select **Delete**.
189191
190192
## Next steps
191-
The next article introduces you to the pre-built T-SQL templates found within SSMS.
193+
The next article introduces you to the prebuilt T-SQL templates found within SSMS.
192194

193-
Advance to the next article to learn more:
195+
Go to the next article to learn more:
194196
> [!div class="nextstepaction"]
195197
> [Next steps](templates-ssms.md)
196198

0 commit comments

Comments
 (0)