TSQL - Create INSERT script for table based on table with existing data

Consider these lines that are inserting data into an existing table:

INSERT INTO foo (ID, FirstName, LastName, LanguageID, Passphrase, EmailAddress, Locked, ResetPassword, SecurityAccessLocationTypesID, LookupListKey, IsDeleted, IsActive, ViewPrivilegedInfo)  
VALUES (2, 'XXX', 'XXXXX', 1, '3ECE475D56C3B6A526E030E9D6ABF7F6DF6D6', 'XXX@XXX.com', 0, 0, 1, 1, 0, 1, 0)

INSERT INTO foo (ID, FirstName, LastName, LanguageID, Passphrase, EmailAddress, Locked, ResetPassword, SecurityAccessLocationTypesID, LookupListKey, IsDeleted, IsActive, ViewPrivilegedInfo)  
VALUES (3, 'XXX', 'XXX', 1, 'A94A8FE5CCB19BA61C4C087982FBBD3', 'XXXk@XXXX.com', 0, 0, 1, 1, 0, 1, 0)

Open in new window


If I have some other table with data that I want to script so that I can TRUNCATE the table, then at some future point import the data again via a script like the sample above -- is there a way to do this via SSMS tools of some sort?

For just a few lines this is not too bad ... but what about 100's or 1000's of rows of data that I want to generate T-SQL INSERT statements for?

I already know about Import/Export of data via the one tool that maps (for example) a flat file to table columns....

I don't want that.  This project calls for us being able to run T-SQL that will insert the data line by line, one row at a time.
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
Right-click on the db name (not on the table name itself).  Select "Tasks", then "Generate Scripts...".  Move past the intro page, select "Select specific database objects", and check the table(s) you want to script, then click "Next".  Fill in where you want to the final script to be written, then click on "Advanced".  Scroll down until you see "Types of data to script" and change it to "Data only", then click "OK".  Click "Next".  Click "Next" again when you're ready to gen the data.

I don't know of any way to limit the number of rows generated from there.  If you want to do only a subset of a table, copy those rows into another table.

This can be somewhat flaky at times, as the GUI always is.  Sometimes it claims to gen the data and then doesn't.  It seems to work much more often if you specify "schema and data" rather than "data only".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
Perfect ... exactly what I wanted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.