views:

4470

answers:

14

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?

I'm thinking of something like in TOAD (for Oracle) where you can right-click on a grid and click Save As->Insert Statements, and it will just dump a big sql script wherever you want.

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.

A: 

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

KM
I'd like the flexibility to edit the data in the INSERTs if I want to. Other than that, no real reason... I need to research the syntax of RESTORE and BACKUP so I can do that from a script.
JosephStyons
+6  A: 

We use this stored procedure - it allows you to target specific tables, and use where clauses. You can find the text here.

For example, it lets you do this: To generate INSERT statements for table 'titles':

EXEC sp_generate_inserts 'titles'
Shane Fulmer
This worked perfectly for me, except that the generated INSERTs don't have a semicolon @ the end. I added that and used it with success. Thanks for answering the qstn!
JosephStyons
I'm glad it worked well for you!
Shane Fulmer
Wow...Handy little script....wonderful
CMB
must be something missing here, that sp doesn't seem to exist in my version of 2008
jcollum
@jcollum - it's actually not a built-in stored procedure. If you follow the link, you can get the text for the stored proc.
Shane Fulmer
Nice. I normally use SSMS Tools Pack in SSMS 2005/2008, but working on a 2000 job, this is really cool.
Gavin
I'm Getting - Msg 536, Level 16, State 5, Procedure sp_generate_inserts, Line 331Invalid length parameter passed to the SUBSTRING function.Msg 536, Level 16, State 5, Procedure sp_generate_inserts, Line 332Invalid length parameter passed to the SUBSTRING function.Msg 50000, Level 16, State 1, Procedure sp_generate_inserts, Line 336No columns to select. There should at least be one column to generate the output Why?
Nimrod Shory
@nimrod: I'm getting the same error. Using Sql Server 2008 and schemas.
Gabe
@Gabriel: Sorry - i didn't get passed this - We are creating the inserts using a C# util.
Nimrod Shory
A: 

There is a script to generate INSERT statements from your data here:

Script Tbl Data to Insert Stmts. - Handles Unicode

RedFilter
+1  A: 

Don't use inserts, use BCP

ShuggyCoUk
Valid in most cases but there can be good reasons for wanting to use inserts.
Steve Homer
A: 

Not sure, if I understand your question correctly.

If you have data in MS-Access, which you want to move it to SQL Server - you could use DTS.
And, I guess you could use SQL profiler to see all the INSERT statements going by, I suppose.

shahkalpesh
A: 

Do you have data in a production database yet? If so, you could setup a period refresh of the data via DTS. We do ours weekly on the weekends and it is very nice to have clean, real data every week for our testing.

If you don't have production yet, then you should create a database that is they want you want it (fresh). Then, duplicate that database and use that newly created database as your test environment. When you want the clean version, simply duplicate your clean one again and Bob's your uncle.

Nick DeVore
+1  A: 
johnnycrash
+2  A: 

Perhaps you can try the SQL Server Publishing Wizard http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en

It has a wizard that helps you script insert statements.

janem
+8  A: 

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/

BinaryHacker
A: 

I have also researched lot on this, but I could not get the concrete solution for this. Currently the approach I follow is copy the contents in excel from SQL Server Managment studio and then import the data into Oracle-TOAD and then generate the insert statements

+1  A: 

Jane Dallaway's stored procedure: http://jane.dallaway.com/downloads/SQL/spu_generateInsert.sql. Documentation is a series of blog posts: http://jane.dallaway.com/blog/labels/spu_generateinsert.html

harriyott
Jane
A: 

I use sqlite to do this. I find it very, very useful for creating scratch/test databases.

sqlite3 foo.sqlite .dump > foo_as_a_bunch_of_inserts.sql

Paul Harrington
+11  A: 

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 Insert statements for all of the data in your table, using no scripts or add-ins to SQL Management Studio 2008:

  1. DATABASE NAME: Right Click
  2. TASKS: GENERATE SCRIPTS
  3. Under Table/View Options: Set SCRIPT DATA = TRUE

You will then get the create table statement and all of the INSERT statements for the data straight out of SSMS.

Mike Ritacco
Awesome! Thank you!
mattdell
A: 

This one so far has worked for me, although they might be bugs I have not discovered yet .

YordanGeorgiev