Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET

Generate insert Statements from a Specific Table Data using SQL Server

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
27 May 2013CPOL3 min read 122.1K   6   7
Generate insert statements from a 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. In this article, I will show you 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 will automatically install the Microsoft SQL Server Data Publishing Wizard at 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

SQLPubWiz location

SQLPubWiz location

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

Database Publishing Wizard

Database Publishing Wizard

Click next and enter the database server connection information accordingly.

Database Publishing Wizard - Database Server Connection

Database Publishing Wizard – Database Server Connection

Click next, and it will ask you to select the database, which you want to generate the data from:

Database Publishing Wizard - Select Database

Database Publishing Wizard – Select Database

Next screen will ask 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”).

Database Publishing Wizard - Choose Object Type

Database Publishing Wizard – Choose Object Type

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

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.

Database Publishing Wizard - Select an Output Location

Database Publishing Wizard – Select an Output Location

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

Database Publishing Wizard - Select Publishing Option

Database Publishing Wizard – Select Publishing Option

Click next and it will generate the insert data script for you.

Database Publishing Wizard - Publishing Progress

Database Publishing Wizard – Publishing Progress

Database Publishing Wizard - Generated Script

Database Publishing Wizard – Generated Script

2. Using SQL Server Generate Script Wizard

SQL Server Generate Script

SQL Server Generate Script

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

SQL Server Generate Script - Wizard Dialog

SQL Server Generate Script – Wizard Dialog

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

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.

SQL Server Generate Script Wizard - Choose Script Option

SQL Server Generate Script Wizard – Choose Script Option

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

SQL Server Generate Script Wizard - Choose Object Type

SQL Server Generate Script Wizard – Choose Object Type

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

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.

SQL Server Generate Script Wizard - Output Option

SQL Server Generate Script Wizard – Output Option

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

SQL Server Generate Script Wizard - Script Summary

SQL Server Generate Script Wizard – Script Summary

SQL Server Generate Script Wizard - Script Progress

SQL Server Generate Script Wizard – Script Progress

SQL Server Generate Script Wizard - Generated Script

SQL Server Generate Script Wizard – Generated Script

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Eyepax IT Consulting (Pvt) Ltd.
Sri Lanka Sri Lanka
Having more than 9 year hands-on industry experience in software development
Responsible for designing, implementing and managing complex software systems with stringent up-time requirement.

Visit my blog

Comments and Discussions

 
QuestionHow to select specific rows from a table Pin
Sergiy Bogdancev28-Sep-15 2:54
Sergiy Bogdancev28-Sep-15 2:54 
QuestionFor SQL 2014 Pin
jonatasm29-Jun-15 5:12
jonatasm29-Jun-15 5:12 
QuestionGenerate scripts using cmd Pin
Sharmin Jinia24-Nov-14 0:32
Sharmin Jinia24-Nov-14 0:32 
QuestionThank you! Pin
TheHelenLee18-Dec-13 10:25
TheHelenLee18-Dec-13 10:25 
AnswerRe: Thank you! Pin
Tharaka MTR18-Dec-13 19:18
professionalTharaka MTR18-Dec-13 19:18 
QuestionThanks Pin
Frans_551294-Jun-13 3:05
Frans_551294-Jun-13 3:05 
AnswerRe: Thanks Pin
Tharaka MTR4-Jun-13 7:09
professionalTharaka MTR4-Jun-13 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.