How to restore to a different database in sql server?

后端 未结 10 1628
忘掉有多难
忘掉有多难 2020-11-29 14:39

I have a backup of Database1 from a week ago. The backup is done weekly in the scheduler and I get a .bak file. Now I want to fiddle with some

10条回答
  •  一个人的身影
    2020-11-29 15:12

    Here is how to restore a backup as an additional db with a unique db name.

    For SQL 2005 this works very quickly. I am sure newer versions will work the same.

    First, you don't have to take your original db offline. But for safety sake, I like to. In my example, I am going to mount a clone of my "billing" database and it will be named "billingclone".

    1) Make a good backup of the billing database

    2) For safety, I took the original offline as follows:

    3) Open a new Query window

    **IMPORTANT! Keep this query window open until you are all done! You need to restore the db from this window!

    Now enter the following code:

    -- 1) free up all USER databases
    USE master;
    GO
    -- 2) kick all other users out:
    ALTER DATABASE billing SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    GO
    -- 3) prevent sessions from re-establishing connection:
    ALTER DATABASE billing SET OFFLINE;
    

    3) Next, in Management Studio, rt click Databases in Object Explorer, choose "Restore Database"

    4) enter new name in "To Database" field. I.E. billingclone

    5) In Source for Restore, click "From Device" and click the ... navigate button

    6) Click Add and navigate to your backup

    7) Put a checkmark next to Restore (Select the backup sets to restore)

    8) next select the OPTIONS page in upper LH corner

    9) Now edit the database file names in RESTORE AS. Do this for both the db and the log. I.E. billingclone.mdf and billingclone_log.ldf

    10) now hit OK and wait for the task to complete.

    11) Hit refresh in your Object Explorer and you will see your new db

    12) Now you can put your billing db back online. Use the same query window you used to take billing offline. Use this command:

    -- 1) free up all USER databases
    USE master; GO
    -- 2) restore access to all users:
    ALTER DATABASE billing SET MULTI_USER WITH ROLLBACK IMMEDIATE;GO
    -- 3) put the db back online:
    ALTER DATABASE billing SET ONLINE;
    

    done!

提交回复
热议问题