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

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

    This can be done using Visual Studio too (at least in version 2013 onwards).

    In VS 2013 it is also possible to filter the list of rows the inserts statement are based on, this is something not possible in SSMS as for as I know.

    Perform the following steps:

    • Open the "SQL Server Object Explorer" window (menu: /View/SQL Server Object Explorer)
    • Open / expand the database and its tables
    • Right click on the table and choose "View data" from context menu
    • This will display the data in the main area
    • Optional step: Click on the filter icon "Sort and filter data set" (the fourth icon from the left on the row above the result) and apply some filter to one or more columns
    • Click on the "Script" or "Script to File" icons (the icons on the right of the top row, they look like little sheets of paper)

    This will create the (conditional) insert statements for the selected table to the active window or file.


    The "Filter" and "Script" buttons Visual Studio 2013:

    0 讨论(0)
  • 2020-11-22 09:57

    Do you have data in a production database yet? If so, you could setup a period refresh of the data via DTS. We do ours weekly on the weekends and it is very nice to have clean, real data every week for our testing.

    If you don't have production yet, then you should create a database that is they want you want it (fresh). Then, duplicate that database and use that newly created database as your test environment. When you want the clean version, simply duplicate your clean one again and Bob's your uncle.

    0 讨论(0)
  • 2020-11-22 09:57

    You can generate INSERT or MERGE statement with this simple and free application I wrote a few years ago:
    Data Script Writer (Desktop Application for Windows)

    Also, I wrote a blog post about these tools recently and approach to leveraging SSDT for a deployment database with data. Find out more:
    Script and deploy the data for database from SSDT project

    0 讨论(0)
  • 2020-11-22 09:58

    Not sure, if I understand your question correctly.

    If you have data in MS-Access, which you want to move it to SQL Server - you could use DTS.
    And, I guess you could use SQL profiler to see all the INSERT statements going by, I suppose.

    0 讨论(0)
  • 2020-11-22 09:59

    Perhaps you can try the SQL Server Publishing Wizard http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en

    It has a wizard that helps you script insert statements.

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

    My contribution to the problem, a Powershell INSERT script generator that lets you script multiple tables without having to use the cumbersome SSMS GUI. Great for rapidly persisting "seed" data into source control.

    1. Save the below script as "filename.ps1".
    2. Make your own modifications to the areas under "CUSTOMIZE ME".
    3. You can add the list of tables to script in any order.
    4. You can open the script in Powershell ISE and hit the Play button, or simply execute the script in the Powershell command prompt.

    By default, the INSERT script generated will be "SeedData.sql" under the same folder as the script.

    You will need the SQL Server Management Objects assemblies installed, which should be there if you have SSMS installed.

    Add-Type -AssemblyName ("Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
    Add-Type -AssemblyName ("Microsoft.SqlServer.ConnectionInfo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
    
    
    
    #CUSTOMIZE ME
    $outputFile = ".\SeedData.sql"
    $connectionString = "Data Source=.;Initial Catalog=mydb;Integrated Security=True;"
    
    
    
    $sqlConnection = new-object System.Data.SqlClient.SqlConnection($connectionString)
    $conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection($sqlConnection)
    $srv = new-object Microsoft.SqlServer.Management.Smo.Server($conn)
    $db = $srv.Databases[$srv.ConnectionContext.DatabaseName]
    $scr = New-Object Microsoft.SqlServer.Management.Smo.Scripter $srv
    $scr.Options.FileName = $outputFile
    $scr.Options.AppendToFile = $false
    $scr.Options.ScriptSchema = $false
    $scr.Options.ScriptData = $true
    $scr.Options.NoCommandTerminator = $true
    
    $tables = New-Object Microsoft.SqlServer.Management.Smo.UrnCollection
    
    
    
    #CUSTOMIZE ME
    $tables.Add($db.Tables["Category"].Urn)
    $tables.Add($db.Tables["Product"].Urn)
    $tables.Add($db.Tables["Vendor"].Urn)
    
    
    
    [void]$scr.EnumScript($tables)
    
    $sqlConnection.Close()
    
    0 讨论(0)
提交回复
热议问题