Exporting data from SQL Server by generating an insert script
I was moving some data between a couple of databases the other day (just data from a couple of the tables). Basically, I'd created a new feature, tested it a bit which had generated some entries in a new database table and I wanted to move these to the test system.
I was about to do my normal "right click source database -> tasks -> export data..." dance when a colleague mentioned that I could just generate an insert script for the data to either a file, my clipboard or a new query editor window. I couldn't believe I hadn't found this gem before... Although it is reasonably hidden! I gave him the Control+Shift+R short cut for refreshing SSMS intellisense local cache so I guess we're even now.
Anyway, the steps:
Open up SQL Server Management Studio
Expand the databases node in the Object Explorer
Right click the database that you want to generate a script for and go to Tasks -> Generate Scripts
Database node context menu
On the dialog that comes up, select what you want to script - in my case, just one table:
Choosing objects to script
Click to go to the next screen, then click "advanced"
The location of the advanced button ;) just in case you couldn't find it
Scroll down the advanced options and change the "types of data to script" option to "Data only" (or whatever you want).
Before changing the types of data to script
Click OK and then finish the wizard. It will generate you a nice script of inserts in batches of 1000 (max number of inserts per batch).
Lovely.
Comments
Post a Comment