Bluedog67

Random Thoughts About SQL Server and My Dog Blue

About Bluedog67

My name is Stephen Horne. I am a well-trained owner of an Australian Cattle Dog named Blue. I also develop software mainly using SQL Server, ASP.NET, and C#. I love data, databases, T-SQL, and turning raw data into actionable information. My plan for this blog is to write about SQL Server and sometimes my dog Blue. Please contact me at stephen at bluedog67 dot com. Follow me on Twitter at bluedog67.

Create INSERT Script for Existing Table Data Using the tablediff.exe Utility

You have a table named MyTable that contains X number of rows. You need a SQL script that performs the CREATE TABLE command and INSERTs the X rows. There are several ways to accomplish this - this blog post is just one way. SQL Server Managament Studio does a great job of generating a CREATE TABLE script but not the INSERT commands. Following is a quick procedure to generate the INSERT commands using the SQL Server command-line utility named tablediff.exe. In SQL Server 2008 this application is located here - assuming default installation (C:\Program Files\Microsoft SQL Server\100\COM).

The tablediff.exe utility is normally used for replication synching but can be repurposed to generate INSERT statements.

First, create an empty copy of MyTable.

SELECT *
INTO dbo.MyTableCopy
FROM dbo.MyTable
WHERE 0 = 1;

Next execute the following command from a Windows command-line. You will need to change directories to where the tablediff utility is located first.

C:\Program Files\Microsoft SQL Server\100\COM>tablediff.exe -sourceserver MyServer -sourcedatabase MyDatabase -sourcetable MyTable -sourceuser sa -sourcepassword sa -destinationserver MyServer -destinationdatabase MyDatabase -destinationtable MyTableCopy -destinationuser sa -destinationpassword sa -f c:\temp\insert_MyTable.sql

Please note that rather than using named SQL Server logins you can use Windows Authentication and exclude the user/password values. Also the C:\temp directory may need to be created if it doesn't already exist. The process fails if the insert_MyTable.sql file already exists.

The insert_MyTable.sql file will contain the INSERT statements for the X number of rows in the original MyTable table. You will have to open the file in a text editor and global replace MyTableCopy with MyTable. If you combine this INSERT command list with the CREATE TABLE script from SSMS you have what you need to recreate the table and the data contained therein.

For more information about the tablediff.exe utility either go to SQL BOL or type in the following at the Windows command-line.

tablediff.exe /?

Please share any feedback that you may have. Thanks!

Categories: SQL Server
Permalink | Comments (1) | Post RSSRSS comment feed

Comments

Giammarco Schisani Ireland

Wednesday, December 23, 2009 8:00 PM

Giammarco Schisani

Hi Stephen,

I have developed a tool called Table Diff, which allows you to perform a similar task but through a nice user interface.

Please let me know if you would like to have a look at it and possibly review it. I can send you a license.

Here you can find additional information: http://www.volpet.com

Thank you,
Giammarco