Hi and welcome to my blog. Im Tasos, a software engineer working in the UK. This is where i share some of my findings related with SQL, c#, asp.net and javascript with you. I hope you find something helpful and Im looking forward to your feedback!

Recent Comments

Popular Posts

Recent Posts

Archives

Post Categories

Blog Stats

  • Posts: 14
  • Comments: 36
  • Trackbacks: 16
  • Articles: 1

How to Script the data of an SQL table using a Stored proc

Wednesday, April 25, 2007 10:40 PM, Filed Under SQL

The Problem:
I find myself quite often in the following situation: I add a couple of tables in a development database, add some data in that table and then I need to script the changes and apply them to the staging and then to the production db.

If you dont create everything by script SQL Server Management Studio can script the Create Table for us, but there is no way to script the data! For this reason I created a stored procedure, which yeah you guessed it, creates all the INSERT statements containing the data of the table.

The Solution:
The following script works on SQL 2005 since it makes use of the coalesce function (sql 2000 fix follows script)
Here you go:

CREATE Proc dbo.ScriptInsertStatements(
      @TableName varchar(128),
      @WhereClause varchar(400) = Null

)
AS

DECLARE @sql varchar(Max)
DECLARE
@sqlColumns varchar(Max)
DECLARE
@SqlColumnValues varchar(Max)

SELECT @sqlColumns = Coalesce(@SqlColumns + ',','') + Column_Name from Information_Schema.Columns where Table_name = @TableName

SELECT @SqlColumnValues = Coalesce(@SqlColumnValues + '+  '','' + ','') +
      CASE
            WHEN Data_Type in ('varchar','nvarchar', 'char', 'nchar', 'datetime' ) THEN  'QuoteName(' + Column_Name + ', '''''''')'
            ELSE 'Cast(' + Column_Name + ' as varchar(MAX))'
      END

FROM

      Information_Schema.Columns where Table_name = @TableName

 
SELECT
@sql = 'Select ''Insert Into ' + @TableName + ' (' + @SqlColumns + ') Values ('' + ' + @SqlColumnValues +' + '')'' FROM ' + @TableName + IsNull(' WHERE ' + @WhereClause, '')

Exec(@sql)

To use the above stored proc simply call it passing the Table Name
e.g.
    Exec dbo.ScriptInsertStatements 'Users'

You may also want to partially script a table based on a condition. You can supply that condition in the whereclause like so:
--only script insert statements of Users Table for records with field 'Active' set to 1
    Exec dbo.ScriptInsertStatements 'Users', 'Active=1' 

Share this post!
digg it
Kick it on DotNetKicks.com

Comments

# re: How to Script the data of an SQL table using a Stored proc, Posted by Tim on 9/10/2007 10:35 PM

Man you don't even know how long I've waited for this since disabling my own Movable Type widget (that doesn't work since Haloscan bypasses that code).

THANK YOU!

# re: How to Script the data of an SQL table using a Stored proc, Posted by Kiran Tikare on 2/19/2008 5:20 PM

You really rocks!!!!!! I too was in need of such thing.

THANK YOU VERY VERY MUCH

# re: How to Script the data of an SQL table using a Stored proc, Posted by Jeremy on 6/6/2008 8:53 PM

The following is an updated script that handles null values:

ALTER Proc dbo.kpm_ScriptInsertStatements(
@TableName varchar(128),
@WhereClause varchar(400) = Null

)
AS

DECLARE @sql varchar(Max)
DECLARE @sqlColumns varchar(Max)
DECLARE @SqlColumnValues varchar(Max)

SELECT @sqlColumns = Coalesce(@SqlColumns + ',','') + Column_Name from Information_Schema.Columns where Table_name = @TableName

Print @sqlColumns

SELECT @SqlColumnValues = Coalesce(@SqlColumnValues + '+ '','' + ','') +
CASE
WHEN Data_Type in ('varchar','nvarchar', 'char', 'nchar', 'datetime' ) THEN 'ISNULL(QuoteName(' + Column_Name + ', ''''''''), ''NULL'')'
ELSE 'ISNULL(Cast(' + Column_Name + ' as varchar(MAX)), ''NULL'')'
END
FROM
Information_Schema.Columns where Table_name = @TableName


SELECT @sql = 'Select ''Insert Into ' + @TableName + ' (' + @SqlColumns + ') Values ('' + ' + @SqlColumnValues +' + '')'' FROM ' + @TableName + IsNull(' WHERE ' + @WhereClause, '')

exec(@sql)

# re: How to Script the data of an SQL table using a Stored proc, Posted by Anastasiosyal on 6/12/2008 2:36 PM

Thanks for posting your updated version Jeremy, much more practical, I have also been using an updated version of the script, I should have posted the changes back to the blog! :)

# Payday loans with extensions., Posted by Payday loans with extensions. on 7/30/2008 11:31 PM

Payday loans with extensions.

# re: How to Script the data of an SQL table using a Stored proc, Posted by Scott on 8/5/2008 12:37 AM

OK I am an idiot - I get no output. It runs no errors - no output. What am I missing????

# re: How to Script the data of an SQL table using a Stored proc, Posted by Scott on 8/5/2008 12:43 AM

Never mind - I dod not realize the table name did not need the traditional 'dbo.' in front of it. Sorry - I am an idiot.

# Xenical diet plan buy., Posted by Xenical diet plan buy. on 8/7/2008 9:42 AM

Buy xenical and propecia online from pharmcom. Xenical diet plan buy. Cheapest xenical buy online.

Comments

Title: *
Name: *
Email: (never displayed)
Website:
Comment: *  
Please add 3 and 6 and type the answer here: