Quantcast
Viewing latest article 12
Browse Latest Browse All 36

Generate insert statements from the specific table data using SQL Server

When you are deploying your application, sometimes you may want to generate SQL script with the insert statement of the specific database table. The following article shows two easiest ways to do this with SQL Server and SQL Server tools.

  1. Using Database Publishing Wizard (“SqlPubWiz.exe”)

If you install Visual Studio 2008 / Visual Studio 2008 Express full version, it automatically installs the Microsoft SQL Server Data Publishing Wizard in the following location.
[SQL Server Installation Location]\90\Tools\Publishing[version]\ SqlPubWiz.exe

If it is not there, you can download it from the following URL
http://go.microsoft.com/fwlink/?LinkId=119368

When you run the “SqlPubWiz.exe”, it opens the database publishing wizard

Image may be NSFW.
Clik here to view.
Database Publishing Wizard

Database Publishing Wizard

Click next and enter the database server connection information accordingly.

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Database Server Connection

Database Publishing Wizard – Database Server Connection

Click next, and it asks you to select the database, which you want to generate the data from

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Select Database

Database Publishing Wizard – Select Database

Next screen asks to select the Object type. You can use this database publishing wizard to generate data and schema both. In here I consider only for the data generation part. Now choose “Tables” as an option and click next. (Make sure to uncheck “Script all objects in the selected database”)

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Choose Object Type

Database Publishing Wizard – Choose Object Type

Then, the next ask to select the table that you want to generate the data from. Select whatever the table you want and click next.

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Choose Tables

Database Publishing Wizard – Choose Tables

Next step asks to specify the output location. You can enter the file name and the location there.

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Select an Output Location

Database Publishing Wizard – Select an Output Location

By clicking Next, It shows to choose the publishing options. There are three publishing types, Schema only, Data only, or Schema and data. Here you have to select “Data only”.

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Select Publishing Option

Database Publishing Wizard – Select Publishing Option

Click next and it generates the insert data script for you.

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Publishing Progress

Database Publishing Wizard – Publishing Progress

Image may be NSFW.
Clik here to view.
Database Publishing Wizard - Generated Script

Database Publishing Wizard – Generated Script

  1. Using SQL Server Generate Script wizard
Image may be NSFW.
Clik here to view.
SQL Server Generate Script

SQL Server Generate Script

When you select “Generate Script” menu item, it opens the Generate SQL Server Scripts Wizard dialog.

Image may be NSFW.
Clik here to view.
SQL Server Generate Script - Wizard Dialog

SQL Server Generate Script – Wizard Dialog

By clicking next, it asks to select the database that you want to generate the script.

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Select Database

SQL Server Generate Script Wizard – Select Database

By clicking Next, It shows to select the script options dialog box. In here, you change the “Script Data” as “True” and click the next button

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Choose Script Option

SQL Server Generate Script Wizard – Choose Script Option

Next screen asks to select the Object type. Now choose “Tables” as an option and click next.

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Choose Object Type

SQL Server Generate Script Wizard – Choose Object Type

Next screen asks you to select the tables that you want to generate the data from. Select whatever the tables and click next

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Choose Tables

SQL Server Generate Script Wizard – Choose Tables

Next step asks to specify the output options. If you select “Script to file” as an option, then you can enter the file name and the location there.

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Output Option

SQL Server Generate Script Wizard – Output Option

Next two steps are very straight forward. It shows the script generation summary, and by clicking next it generates the script with insert statements for the selected tables.

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Script Summary

SQL Server Generate Script Wizard – Script Summary

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Script Progress

SQL Server Generate Script Wizard – Script Progress

Image may be NSFW.
Clik here to view.
SQL Server Generate Script Wizard - Generated Script

SQL Server Generate Script Wizard – Generated Script


Filed under: CodeProject, SQL Tagged: Database, database problems, database publishing, microsoft sql server, software, SQL 2005, SQL 2008, SQL 2012, SQL Server, sql server data, sql server tools, technology Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 12
Browse Latest Browse All 36

Trending Articles