Tool to export result set from SQL to Insert statements?

Personally, I would just write a select against the table and generate the inserts myself. Piece of cake.

For example:

SELECT  'insert into [pubs].[dbo].[authors](
                    [au_id], 
                    [au_lname], 
                    [au_fname], 
                    [phone], 
                    [address], 
                    [city], 
                    [state], 
                    [zip], 
                    [contract])
    values( ''' + 
    [au_id] + ''', ''' + 
    [au_lname] + ''', ''' +
    [au_fname] + ''', ''' +
    [phone] + ''', ''' +
    [address] + ''', ''' +
    [city] + ''', ''' +
    [state] + ''', ''' +
    [zip] + ''', ' +
    cast([contract] as nvarchar) + ');'
FROM    [pubs].[dbo].[authors]

will produce

insert into [pubs].[dbo].[authors](
                    [au_id], 
                    [au_lname], 
                    [au_fname], 
                    [phone], 
                    [address], 
                    [city], 
                    [state], 
                    [zip], 
                    [contract])
    values( '172-32-1176', 'White', 'Johnson', '408 496-7223', '10932 Bigge Rd.', 'Menlo Park', 'CA', '94025', 1);
insert into [pubs].[dbo].[authors](
                    [au_id], 
                    [au_lname], 
                    [au_fname], 
                    [phone], 
                    [address], 
                    [city], 
                    [state], 
                    [zip], 
                    [contract])
    values( '213-46-8915', 'Green', 'Marjorie', '415 986-7020', '309 63rd St. #411', 'Oakland', 'CA', '94618', 1);
... etc ...

A couple pitfalls:

  1. Don't forget to wrap your single quotes
  2. This assumes a clean database and is not SQL Injection safe.

take a look at the SSMS Tools Pack add in for SSMS which allows you to do just what you need.

Tags:

Sql

Sql Server