Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

backup - How to restore to a different database in sql server?

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 data so I need to restore it to a different database - Database2.

I have seen this question: Restore SQL Server database in same pc with different name and the recommended step is to rename the original db, but I am out of that option as I am in the production server and I cant really do it.

Is there any other way of restoring it to Database2, or atleast, how do I browse through the data of that .bak file?

thanks.

ps: the second answer from the above link looked promising but it keeps terminating with error:

Restore Filelist is terminating abnormally

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create a new db then use the "Restore Wizard" enabling the Overwrite option or:

View the contents of the backup file:

RESTORE FILELISTONLY FROM DISK='c:your.bak'

note the logical names of the .mdf & .ldf from the results, then:

RESTORE DATABASE MyTempCopy FROM DISK='c:your.bak'
WITH 
   MOVE 'LogicalNameForTheMDF' TO 'c:MyTempCopy.mdf',
   MOVE 'LogicalNameForTheLDF' TO 'c:MyTempCopy_log.ldf'

This will create the database MyTempCopy with the contents of your.bak.

(Don't create the MyTempCopy, it's created during the restore)


Example (restores a backup of a db called 'creditline' to 'MyTempCopy'):

RESTORE FILELISTONLY FROM DISK='e:mssqlackupcreditline.bak'

>LogicalName
>--------------
>CreditLine
>CreditLine_log

RESTORE DATABASE MyTempCopy FROM DISK='e:mssqlackupcreditline.bak'
WITH 
   MOVE 'CreditLine' TO 'e:mssqlMyTempCopy.mdf',
   MOVE 'CreditLine_log' TO 'e:mssqlMyTempCopy_log.ldf'

>RESTORE DATABASE successfully processed 186 pages in 0.010 seconds (144.970 MB/sec).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...