11

Using SQL Server Management Studio is there a way I can select one or more rows in the grid of select results and have SQL Server Mangement Studio generate one or more insert statements (one for each row selected) which would insert that data into a table with the same schema?

Edit: I know how to create one manually, but I was hoping there would be something that would create it automatically for me. If you are familiar with Toad there is a way to have Toad generate inserts based on data in the results pane and I was hoping SSMS had an equivalant function.

Shane Wealti
  • 2,252
  • 3
  • 19
  • 33
  • 2
    possible duplicate of [Converting Select results into Insert script](http://stackoverflow.com/questions/4526461/converting-select-results-into-insert-script) – Shane Wealti Jan 10 '12 at 15:28

4 Answers4

13

Try to save the query result into a disposable table.

For example:

SELECT * INTO disposable_customer_table FROM customer_table WHERE id IN (in range of something)

Then do a db -> Tasks -> Generate Scripts.

  • Select specific database objects.
  • Choose disposable_customer_table from the list of table names.
  • Choose Save to file.
  • Make sure to do an Advance setup and select "Data only" from the 'Types of data to script'.

Tweak the result file and rename the disposable_customer_table back to the original table name.

Clean it up and drop the disposable_customer_table.

sakadas
  • 539
  • 4
  • 13
6
 select 'insert into tableB values (', tableA.x ,',',tableA.y,',',tableA.z,')' from tableA
dov.amir
  • 11,489
  • 7
  • 45
  • 51
4

I think you have two options here:

  1. Create your inserts manually. For instance:

    select Name, Surname,
      'insert into Person (Name,surname) values ('''+Name+''','''+Surname+')'
      from Person
    

    This gets you the results and, in the last column, the insert script for the row. You can then select and paste it in an Editor window.

  2. Right click on the db -> Tasks -> Generate Scripts. Press then Advance and select "Data Only" (Default is Schema Only).

Pang
  • 9,564
  • 146
  • 81
  • 122
fgpx78
  • 1,270
  • 2
  • 10
  • 8
0

Perform your query and right click on the blank area where the column headers meet the row number in the Results view.

You can then select Script Grid Results:

enter image description here

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • 5
    what version of ssms are you using? I am using 2012 and I do not see the SCRIPT GRID RESULTS or SEARCH GRID RESULTS options. – greg Nov 05 '13 at 22:28
  • 1
    I don't have it either. I think that functionality is provided by a plug-in. OP? – Shane Wealti Nov 18 '13 at 21:02
  • 3
    this is done by the [SSMS Tools Pack](http://www.ssmstoolspack.com/), unfortunately the SMSS 2012 version is not free – tbt Feb 26 '14 at 14:00