What is the best way to auto-generate INSERT statements for a SQL Server table?

前端 未结 23 1391
难免孤独
难免孤独 2020-11-22 09:47

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 table

相关标签:
23条回答
  • 2020-11-22 10:14

    Don't use inserts, use BCP

    0 讨论(0)
  • 2020-11-22 10:15

    If you need a programmatic access, then you can use an open source stored procedure `GenerateInsert.

    INSERT statement(s) generator

    Just as a simple and quick example, to generate INSERT statements for a table AdventureWorks.Person.AddressType execute following statements:

    USE [AdventureWorks];
    GO
    EXECUTE dbo.GenerateInsert @ObjectName = N'Person.AddressType';
    

    This will generate the following script:

    SET NOCOUNT ON
    SET IDENTITY_INSERT Person.AddressType ON
    INSERT INTO Person.AddressType
    ([AddressTypeID],[Name],[rowguid],[ModifiedDate])
    VALUES
     (1,N'Billing','B84F78B1-4EFE-4A0E-8CB7-70E9F112F886',CONVERT(datetime,'2002-06-01 00:00:00.000',121))
    ,(2,N'Home','41BC2FF6-F0FC-475F-8EB9-CEC0805AA0F2',CONVERT(datetime,'2002-06-01 00:00:00.000',121))
    ,(3,N'Main Office','8EEEC28C-07A2-4FB9-AD0A-42D4A0BBC575',CONVERT(datetime,'2002-06-01 00:00:00.000',121))
    ,(4,N'Primary','24CB3088-4345-47C4-86C5-17B535133D1E',CONVERT(datetime,'2002-06-01 00:00:00.000',121))
    ,(5,N'Shipping','B29DA3F8-19A3-47DA-9DAA-15C84F4A83A5',CONVERT(datetime,'2002-06-01 00:00:00.000',121))
    ,(6,N'Archive','A67F238A-5BA2-444B-966C-0467ED9C427F',CONVERT(datetime,'2002-06-01 00:00:00.000',121))
    SET IDENTITY_INSERT Person.AddressType OFF
    
    0 讨论(0)
  • 2020-11-22 10:16

    I use sqlite to do this. I find it very, very useful for creating scratch/test databases.

    sqlite3 foo.sqlite .dump > foo_as_a_bunch_of_inserts.sql

    0 讨论(0)
  • 2020-11-22 10:18

    I used this script which I have put on my blog (How-to generate Insert statement procedures on sql server).

    So far has worked for me, although they might be bugs I have not discovered yet .

    0 讨论(0)
  • 2020-11-22 10:21

    I'm using SSMS 2008 version 10.0.5500.0. In this version as part of the Generate Scripts wizard, instead of an Advanced button, there is the screen below. In this case, I wanted just the data inserted and no create statements, so I had to change the two circled propertiesScript Options

    0 讨论(0)
提交回复
热议问题