질문
We are writing a new application, and while testing, we will need a bunch of dummy data. I've added that data by using MS Access to dump excel files into the relevant tables.
Every so often, we want to "refresh" the relevant tables, which means dropping them all, re-creating them, and running a saved MS Access append query.
The first part (dropping & re-creating) is an easy sql script, but the last part makes me cringe. I want a single setup script that has a bunch of INSERTs to regenerate the dummy data.
I have the data in the tables now. What is the best way to automatically generate a big list of INSERT statements from that dataset?
I'm thinking of something like in TOAD (for Oracle) where you can right-click on a grid and click Save As->Insert Statements, and it will just dump a big sql script wherever you want.
The only way I can think of doing it is to save the table to an excel sheet and then write an excel formula to create an INSERT for every row, which is surely not the best way.
I'm using the 2008 Management Studio to connect to a SQL Server 2005 database
답변
As mentioned by @Mike Ritacco but updated for SSMS 2008 R2
- Right click on the database name
- Choose Tasks > Generate scripts
- Depending on your settings the intro page may show or not
- Choose 'Select specific database objects',
- Expand the tree view and check the relevant tables
- Click Next
- Click Advanced
- Under General section, choose the appropriate option for 'Types of data to script'
- Complete the wizard
You will then get all of the INSERT statements for the data straight out of SSMS