As we said previously, Create Scripts are SQL scripts that create new database objects, including tables, views, stored procedures, and constraints. They are normally used to set up an installation or revert an existing site (such as a development server) to its pristine state.
You can also display the Generate Create Scripts dialog box by dragging and dropping from the Server Explorer to a folder in the Solution Explorer. Here, too, you can drag either the entire database or individual database objects. If you select one or more individual objects, such as a table and its relevant stored procedures, the Generate Create Scripts dialog appears configured for just the selected object(s). However, if you select the entire database, or even a single folder within the database, such as the Tables or Views folder, it in fact behaves as if you dragged the entire database. If you want to script more than just a single object, you can also start by right-clicking on the Create Scripts folder
To generate Create Scripts, do the following.
Open the Server Explorer and right-click on the item for which you want to generate the script. This item can be either the entire database or an individual database object (table, view, stored procedure, or function). For this example, select the entire Novelty database.
Select the Generate Create Script menu item from the context menu displayed. After you successfully enter your security credentials in the SQL Server Login dialog box, the Generate Create Scripts dialog box is displayed.
If you select a specific database object, the Generate Create Scripts dialog box appears, configured for only that object. If you select the entire database, as in Figure 8.3, the dialog box appears, showing all the objects available on the database but without any of them selected for scripting. This result is shown in Figure 8.3.
Figure 8.3 The Generate Create Scripts dialog box for the entire Novelty database
On the General tab, you can specify which objects are to be included in the generated script by selecting one or more objects from the Objects on the Novelty panel and adding them to the Objects to be scripted panel by either double-clicking on an object or the Add button. Entire groups of objects, such as all tables or all views (or even all the database objects), can be specified by checking one or more of the checkboxes in the top portion of the tab. For this example, check the Script all objects checkbox.
Select the Formatting tab. In addition to the default settings, check the Include descriptive headers in the script files checkbox.
Click on the OK button. Doing so displays the Browse for Folder dialog box so that you can specify where to save the script file(s). Note that it defaults to the Create Scripts directory of the current project, but you can change this setting to whatever you want. Accept the default by clicking on the OK button.
Because we had you choose to script all the database objects, many script files are created and added to the project, as shown in Figure 8.4. We could have had you choose to have all the scripts in a single file by selecting the Create one file option on the Options tab of the Generate Create Scripts dialog box.
Figure 8.4 The Solution Explorer filled with Create Scripts for the Novelty database
You can even generate the script for creating the database itself by checking the Script database option on the Options tab of the Generate Create Scripts dialog box.
Our database project, NoveltyData, now contains a set of scripts that we can run to create all the database objects for the Novelty database. In the Running
You can view (and modify) the contents of a script file by double-clicking on it in the Solution Explorer. Figure 8.5 shows the script dbo.tblOrder.tab that creates the tblOrder table in the Novelty database.
Figure 8.5 The generated script to create the tblOrder table.
Note, however, that these scripts create only the database schema and do not populate the newly created tables with any data. In the Command Files section, we show how to copy a table's data.