SQL Server, How to generate an `INSERT` statement

2019-08-11 19:52发布

问题:

Is there a way to generate an INSERT statement for the rows in an existing table?

I have a table in SQL Server 2008 with 150 rows. I am wondering if something like the following is possible to get from that table.

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'); 

The SQL script should include all the 150 rows to insert.

回答1:

Here are the steps:-

  1. Right click on your database in SSMS
  2. Choose Tasks>Generate Scripts
  3. Select specific database objects - choose the table you want to script. Click next
  4. Select 'Save to new query window'. Click the 'Advanced button'
  5. Change 'Types of data to script' to 'Data only'.
  6. Click OK, Next, Next, Finish.

The query editor window should contain the script you need.