Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an insert script from select results

Tags:

ssms

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.

like image 393
Shane Wealti Avatar asked Jan 10 '12 14:01

Shane Wealti


2 Answers

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.

like image 173
sakadas Avatar answered Sep 29 '22 13:09

sakadas


 select 'insert into tableB values (', tableA.x ,',',tableA.y,',',tableA.z,')' from tableA
like image 35
dov.amir Avatar answered Sep 29 '22 12:09

dov.amir