Scripting Objects

The most common argument I hear against EM is that creating objects through a GUI as opposed to script-based management means you don't have a saved script to re-create the object if required. Fortunately, EM allows you to create and save a script to create a single object such as a view or all objects within a particular database. To create a script, select the object, right-click, and from the All Tasks menu, select Generate SQL Script. This will open the Generate SQL Scripts dialog box, as shown in Figure 4.29.

Figure 4.29. The Generate SQL Scripts dialog box.


The Preview button allows you to see the format of the script, as seen in Figure 4.30.

Figure 4.30. Previewing the script.


To set up the format of the script, use the Formatting tab to set whether drop commands should be included. This is illustrated in Figure 4.31.

Figure 4.31. The Formatting tab of the Create Script dialog box.


Additional options can be set on the Options tab. One complaint I often hear is that the objects are scripted but not the actual database. This is available under Options, but is not selected by default. As a matter of fact, if you don't select user objects and check the Script Database box, you will generate a script that just creates the database structure. Figure 4.32 shows the Options tab.

Figure 4.32. The Options tab of the Create Script dialog box.


    Part III: SQL Server Administration
    Part IV: Transact-SQL
    Part V: SQL Server Internals and Performance Tuning
    Part VI: Additional SQL Server Features