Thursday, August 1, 2013

SQL Server: Generate Insert Statement Script

When running reports, for example, the same insert statements need to be created over and over again. SQL Server includes a great feature to generate these insert statements automatically.
For this example, I be working with the emp table with 9 rows in my database. Now, many months later, I need to generate an insert statement for this table.
1.Open SQL Server Management Studio and go to Object Explorer.
2.Right click on your database. Go to Tasks then Generate Scripts.

3.You will find a window titled “Generate and Publish Scripts” and an introduction is displayed. Just click on Next to proceed.
4.In the Choose Objects window select the radio button Select specific database objectsand expand the Tables tree structure. Select the table name for which you are going
to generate the insert statements. Then click on Next. In my example, I am selecting the emp table below.

5.  Now in Set Scripting Options window, you need specify how you are going to save your script. Let save to a file by selecting Save script to a specific location and then Save to file with Single file option. You will need to make sure you note the path. Then select theAdvanced button.

6.Now in “Advanced Scripting Options” window in General go to Types of data to script and select Data only. Click on OK and then click on Next.

7.Now you will find the Summary window which shows the information about the selected database, tables, and target file path. Click on Next.
8.Now in Save or Publish Scripts window will show the results of success.
9.In the target file you will find the insert statements for the table.