Best way to copy a database (SQL Server 2008)

后端 未结 8 1677
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 06:50

Dumb question - what\'s the best way to copy instances in an environment where I want to refresh a development server with instances from a production server?

I\'v

相关标签:
8条回答
  • 2020-12-04 07:20

    Its hard to detach your production dB or other running dB's and deal with that downtime, so I almost always use a Backup / restore method.

    If you also want to make sure to keep your login's in sync check out the MS KB article on using the stored proc sp_help_revlogin to do this.

    0 讨论(0)
  • 2020-12-04 07:26

    UPDATE:
    My advice below tells you how to script a DB using SQL Server Management Studio, but the default settings in SSMS miss out all sorts of crucial parts of a database (like indexes and triggers!) for some reason. So, I created my own program to properly script a database including just about every type of DB object you may have added. I recommend using this instead. It's called SQL Server Scripter and it can be found here:
    https://bitbucket.org/jez9999/sqlserverscripter


    I'm surprised no-one has mentioned this, because it's really useful: you can dump out a database (its schema and data) to a script, using SQL Server Management Studio.

    Right-click the database, choose "Tasks | Generate Scripts...", and then select to script specific database objects. Select the ones you want to copy over to the new DB (you probably want to select at least the Tables and Schemas). Then, for the "Set Scripting Options" screen, click "Advanced", scroll down to "Types of data to script" and select "Schema and data". Click OK, and finish generating the script. You'll see that this has now generated a long script for you that creates the database's tables and inserts the data into them! You can then create a new database, and change the USE [DbName] statement at the top of the script to reflect the name of the new database you want to copy the old one to. Run the script and the old database's schema and data will be copied to the new one!

    This allows you to do the whole thing from within SQL Server Management studio, and there's no need to touch the file system.

    0 讨论(0)
  • 2020-12-04 07:26

    I run an SP to DROP the table(s) and then use a DTS package to import the most recent production table(s) onto my development box. Then I go home and come back the following morning. It's not elegant; but it works for me.

    0 讨论(0)
  • 2020-12-04 07:27

    The fastest way to copy a database is to detach-copy-attach method, but the production users will not have database access while the prod db is detached. You can do something like this if your production DB is for example a Point of Sale system that nobody uses during the night.

    If you cannot detach the production db you should use backup and restore.

    You will have to create the logins if they are not in the new instance. I do not recommend you to copy the system databases.

    You can use the SQL Server Management Studio to create the scripts that create the logins you need. Right click on the login you need to create and select Script Login As / Create.

    This will lists the orphaned users:

    EXEC sp_change_users_login 'Report'
    

    If you already have a login id and password for this user, fix it by doing:

    EXEC sp_change_users_login 'Auto_Fix', 'user'
    

    If you want to create a new login id and password for this user, fix it by doing:

    EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'
    
    0 讨论(0)
  • 2020-12-04 07:27

    Below is what I do to copy a database from production env to my local env:

    1. Create an empty database in your local sql server
    2. Right click on the new database -> tasks -> import data
    3. In the SQL Server Import and Export Wizard, select product env's servername as data source. And select your new database as the destination data.
    0 讨论(0)
  • 2020-12-04 07:35

    If you want to take a copy of a live database, do the Backup/Restore method.

    [In SQLS2000, not sure about 2008:] Just keep in mind that if you are using SQL Server accounts in this database, as opposed to Windows accounts, if the master DB is different or out of sync on the development server, the user accounts will not translate when you do the restore. I've heard about an SP to remap them, but I can't remember which one it was.

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