We are writing a new application, and while testing, we will need a bunch of dummy data. I've added that data by using MS Access to dump excel files into the relevant tables.
Every so often, we want to "refresh" the relevant tables, which means dropping them all, re-creating them, and running a saved MS Access append query.
The first part (dropping & re-creating) is an easy sql script, but the last part makes me cringe. I want a single setup script that has a bunch of INSERTs to regenerate the dummy data.
I have the data in the tables now. What is the best way to automatically generate a big list of INSERT statements from that dataset?
The only way I can think of doing it is to save the table to an excel sheet and then write an excel formula to create an INSERT for every row, which is surely not the best way.
I'm using the 2008 Management Studio to connect to a SQL Server 2005 database.
My contribution to the problem, a Powershell INSERT script generator that lets you script multiple tables without having to use the cumbersome SSMS GUI. Great for rapidly persisting "seed" data into source control.
By default, the INSERT script generated will be "SeedData.sql" under the same folder as the script.
You will need the SQL Server Management Objects assemblies installed, which should be there if you have SSMS installed.
This can be done using
Visual Studio
too (at least in version 2013 onwards).In VS 2013 it is also possible to filter the list of rows the inserts statement are based on, this is something not possible in SSMS as for as I know.
Perform the following steps:
This will create the (conditional) insert statements for the selected table to the active window or file.
The "Filter" and "Script" buttons Visual Studio 2013:
You can use SSMS Tools Pack (available for SQL Server 2005 and 2008). It comes with a feature for generating insert statements.
http://www.ssmstoolspack.com/
why not just backup the data before your work with it, then restore when you want it to be refreshed?
if you must generate inserts try: http://vyaskn.tripod.com/code.htm#inserts
Microsoft should advertise this functionality of SSMS 2008. The feature you are looking for is built into the Generate Script utility, but the functionality is turned off by default and must be enabled when scripting a table.
This is a quick run through to generate the
INSERT
statements for all of the data in your table, using no scripts or add-ins to SQL Management Studio 2008:You will then get the
CREATE TABLE
statement and all of theINSERT
statements for the data straight out of SSMS.If you need a programmatic access, then you can use an open source stored procedure `GenerateInsert.
INSERT statement(s) generator
Just as a simple and quick example, to generate INSERT statements for a table
AdventureWorks.Person.AddressType
execute following statements:This will generate the following script: