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

前端 未结 23 1785
难免孤独
难免孤独 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: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
    

提交回复
热议问题