You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ssms/tutorials/scripting-ssms.md
+87-85Lines changed: 87 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
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"
4
4
keywords: SQL Server, SSMS, SQL Server Management Studio, Scripts, Scripting
5
5
author: MashaMSFT
6
6
ms.author: mathoma
@@ -23,174 +23,176 @@ helpviewer_keywords:
23
23
- "scripts [SQL Server], SQL Server Management Studio"
24
24
---
25
25
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:
28
28
29
29
> [!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)
32
32
> * Tables
33
33
> * Stored procedures
34
34
> * Extended events
35
35
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.
37
37
38
38
39
39
## 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.
41
41
42
42
- Install [SQL Server Management Studio](https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms).
43
43
- 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).
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).
46
47
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.
49
48
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.
52
54
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**:
54
56
55
-

57
+

56
58
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**:
59
61
60
-

61
-
6. Review the T-SQL populated in the query window:
3. Select the option **Script** and **Script Action to Clipboard**:
78
+
3. Select **Script** and **Script Action to Clipboard**:
77
79
78
-

80
+

79
81
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**.)
81
83
82
-

84
+

83
85
5. Select **Execute** to execute the query and shrink the transaction log.
84
86
85
87
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*.
-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*.
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.
102
104
6. Select **Replace All** to replace all instances of *AdventureWorks2016* with *AdventureWorks2016a*.
103
105
104
-

106
+

105
107
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.
107
109
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.
110
112
2. Expand the **Databases** node.
111
-
3. Right-click the **AdventureWorks2016** database > **Tasks** > **Generate Scripts**:

115
+

114
116
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**.
116
118
117
-

119
+

118
120
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.
120
122
121
-
a. Select the option to **Save to new query Window**.
123
+
a. Select **Save to new query window**.
122
124
123
-
b. Select **Advanced** and ensure these options are set:
125
+
b. Select **Advanced** and make sure these options are set:
124
126
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*.
> 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).
134
135
135
136
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*.
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.
146
144
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.
148
149
2. Expand your **Databases** node.
149
-
3. Expand your **AdventureWorks** database node.
150
+
3. Expand your **AdventureWorks2016** database node.
150
151
4. Expand your **Tables** node.
151
-
5. Right-click the **dbo.ErrorLog** > **Script Table as** > **Drop and Create To** > **New Query Editor Window**:
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.
156
157
157
158
>[!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.
159
160
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.
162
163
163
-
1. Connect to your SQL Server.
164
+
1. Connect to a server that's running SQL Server.
164
165
2. Expand your **Databases** node.
165
166
3. Expand your **Programmability** node.
166
167
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**:
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
+

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.
182
184
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.
> 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 alreadyrunning **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**.
189
191
190
192
## 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.
0 commit comments