alkampfer on March 31st, 2009

Sometimes I need to generate insert statement, taking data from a starting database. Suppose you need to create sql installation scripts to create a database from scratch, quite often you need also to insert some initial data into some tables.

Sql server management studio does not provide a simple way to take a table and script all its content into INSERT statement, but a simple solution can be found here. This solution is really simple, it creates a stored into the database that can be used to generate data Es.

exec dbo.insertgenerator MyTableName

And this produces

INSERT DeviceType(IdDeviceType,Description) VALUES('0A2D5544-ACEE-DD11-A239-0050569F32EE','aaa')
INSERT DeviceType(IdDeviceType,Description) VALUES('10B8DBAD-42D6-46E5-B787-531CEEFF7526','bbb')
INSERT DeviceType(IdDeviceType,Description) VALUES('42BDCDBE-B384-4323-8F1E-CB9367A65075','ccc')

This is really what I need, great script.

alk.

Tags:

kick it on DotNetKicks.com

One Response to “Generate insert data for sql server tables”

  1. Most data bases can generate DDL for any object but not a lot of them allow generation of INSERT statements for the table data.
    The workaround is to make use of ETL Tools for transferring data across servers. However, there exists a need to generate INSERT statements from the tables for porting data.
    Simplest example is when small or large amount of data needs to be taken out on a removable storage media and copied to a remote location, INSERT..VALUES statements come handy.

    There is a number of scripts available to perform this data transformation task. The problem with those scripts that all of them database specific and they do not work with textiles

    Advanced ETL processor can generate Insert scripts from any data source including text files
    http://www.dbsoftlab.com/gener.....ments.html