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

后端 未结 3 714
眼角桃花
眼角桃花 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.

提交回复
热议问题