Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create SQL INSERT Script with values gathered from table

I need to create a INSERT script in order to insert in another database the same data.
If in SQL Server I select "Script table as > INSERT To" I can easily recreate the skeleton for the INSERT statement. However since I have several records to migrate, I would prefer to avoid having to insert the values manually.

Therefore is there any way to "automatically" get the INSERT script with also the values (coming from the target table) filled in?

I know this could be done with SSIS, but I am wondering whether it would be possible as well with a quicker solution.

like image 483
Francesco Avatar asked Sep 22 '11 12:09

Francesco


People also ask

How do you create an insert statement for the data of the table?

Steps To Auto Generate INSERT StatementsFrom the right-click menu, go to Tasks >> Generate Scripts... In the Generate and Publish Scripts pop-up window, press Next to choose objects screen. Now, the choose objects screen, choose Select specific database objects and choose the tables you want to script.


2 Answers

You can do this with SSMS.

1 - Right-click your database in Object Explorer.
2 - Select Tasks/Generate Scripts...
3 - On the Set Scripting Options page, click the Advanced button and make sure Types of data to script is set to Data only.

The resulting script will have a USE DATABASE statement at the top. Change this to the database you would like to insert the data into.

like image 95
D'Arcy Rittich Avatar answered Oct 17 '22 09:10

D'Arcy Rittich


Use the free SSMS tools pack to "generate insert statements"?

Or in SSMS (don't have it on this PC to confirm) the export wizards allows you to "script data" too

like image 39
gbn Avatar answered Oct 17 '22 10:10

gbn