Converting Select results into Insert script - SQL Server

Captain Comic picture Captain Comic · Dec 24, 2010 · Viewed 186.3k times · Source

I have SQL Server 2008, SQL Server Management Studio.

I need to select data from Table1 in database1. Then I have to edit some values in the results and insert values into Table1 in database2.

Or let me put it other way.

How can I convert the data in one table into insert script.

Answer

KT. picture KT. · May 2, 2016

Here is another method, which may be easier than installing plugins or external tools in some situations:

  • Do a select [whatever you need]INTO temp.table_namefrom [... etc ...].
  • Right-click on the database in the Object Explorer => Tasks => Generate Scripts
  • Select temp.table_name in the "Choose Objects" screen, click Next.
  • In the "Specify how scripts should be saved" screen:
    • Click Advanced, find the "Types of data to Script" property, select "Data only", close the advanced properties.
    • Select "Save to new query window" (unless you have thousands of records).
  • Click Next, wait for the job to complete, observe the resulting INSERT statements appear in a new query window.
  • Use Find & Replace to change all [temp.table_name] to [your_table_name].
  • drop table [temp.table_name].