How do I generate scripts that will rebuild my MS SQL Server 2005 database with data?

后端 未结 3 711
眼角桃花
眼角桃花 2021-01-14 06:57

I have a SQL Server 2005 database, that I would like to be able to recreate at a moment\'s notice. I want to be able to point to my database and generate a complete set of s

相关标签:
3条回答
  • 2021-01-14 07:10

    What I always do is let MS SQL Management Studio create the script to rebuild database und empty tables. Then I use another script to generate a ms-dos batch file to export/import the data via "bcp". See sql below.

    /* this is used to export */
    use databaseXXX
    select ('bcp databaseXXX..' + name + ' OUT ' + name + ' /eErrors.txt /b100 /n /Usa /Ppwd /Sserver') as bcp 
    from 
      sysobjects 
    where 
      type = 'U' 
    order by 
      [name]
    
    
    /* this is used to import */
    use databaseXXX
    select ('bcp databaseXXX..' + name + ' IN ' + name + ' /E /eErrors.txt /b100 /n /Usa /Ppwd /Sserver') as bcp 
    from 
      sysobjects 
    where 
      type = 'U' 
    order by 
      [name]
    

    Works for me everytime and is quick. If you save the table generation script in a file you can put this also into a batch file via sqlcmd command.

    0 讨论(0)
  • 2021-01-14 07:23

    The Database Publishing Wizard is a great little tool for this. Its OSS and free, which is hard to beat.

    0 讨论(0)
  • 2021-01-14 07:24

    Check the following for a procedure that will create a script that will generate a table and all its data. You could wrap this up in another stored proc that iterated all tables and generate a single large script that will regenerate everything from scratch.

    http://anastasiosyal.com/archive/2007/04/25/5.aspx

    Edit: Seems Will has found an even better solution +1 to Will

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