开发者

Quickest way to restore a record from a SQL Server 2008 MDF file

开发者 https://www.devze.com 2023-04-05 05:49 出处:网络
I was wondering what the best approach would be to restoring a single record from an MDF file (generated as backup on the live instance) into the live SQL Serv开发者_JS百科er database.

I was wondering what the best approach would be to restoring a single record from an MDF file (generated as backup on the live instance) into the live SQL Serv开发者_JS百科er database.

I know about the process of attaching the file to the database and have read quite a bit about completely restoring, but how about selecting a single record from one of the tables and inserting it back into the same table on the live instance?

I could always create the new record from scratch myself based on the resulting row from the select statement, but I am sure that there has got to be a smarter and cleaner approach to such a simple task.

Thanks a bunch in advance, looking forward to your answers.

Cheers.


You cannot simply read a record out of an MDF file, you need to attach it or restore it to a database.


Natively, you can't. However, Red Gate has a product called Virtual Restore that allows you to mount a database from a backup.


Is this for right now or for future planning? If the latter, then you can utilize database snapshots.


Depending on what kind of flexibility you have on the live server, you could always just attach the backup database under a different name on the live or another linked server and then just select the record you want straight in to the equivalent table in the live database.

How viable this is depends entirely on the primary key. If it is an auto-generated identity column, selecting it in will give a different primary key which may have undesirable results on any linked records you may also want to add, the new primary key would have to be taken in to account.

Example of query

insert into originaldb.dbo.Persons 
select * from backupdb.dbo.Persons where PersonId = '654G'

originaldb.dbo.Persons is the original table that you want to select into.

backupdb.dbo.Persons is your restored backup table.

You'll need to modify this query a little if you are not selecting the entire row but that is the gist of it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号