Scripting a Table

SQL Server Management Studio can create scripts to select, insert, update, and delete tables, and to create, alter, drop, or execute stored procedures.

Sometimes you want a script with multiple options, such as drop a procedure and then create a procedure, or create a table then alter a table. To create combined scripts, save the first script to a Query Editor window and the second to the clipboard so you can paste it into the window after the first script.

Creating an Insert Script

To create the insert script for a table

  1. In Object Explorer, expand your server, expand Databases, expand AdventureWorks, expand Tables, right-click HumanResources.Employee, and then point to Script Table As.

  2. The shortcut menu has six scripting options: CREATE To, DROP To, SELECT To, INSERT To, UPDATE To, and DELETE To. Point to UPDATE To, and then click New Query Editor Window.

  3. A new Query Editor window opens, makes a connection, and presents the entire update statement.

    This exercise demonstrates how the scripting feature can do more than just script the creation of a table or stored procedure. This new feature can help you quickly add data manipulation scripts to your project and easily script execution of stored procedures. This can be a big timesaver for tables and procedures with many fields.

Next Task in Lesson

Summary: Writing Transact-SQL