简体   繁体   English

从不同数据库备份/还原导致无法获得还原失败的独占访问权限

[英]Backup/Restore from different database causing Restore failed exclusive access could not be obtained

I have a database A. I have taken a backup of database A called A.bak. 我有一个数据库A.我已经备份了一个名为A.bak的数据库A. I created a new database B. Now, I right click and Restore B from A.bak. 我创建了一个新的数据库B.现在,我右键单击并从A.bak恢复B. In the Restore Dialog, I checked overwrite existing database and change the LogicalFileName from C:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLSERVER2012\\MSSQL\\DATA\\A.mdf to C:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLSERVER2012\\MSSQL\\DATA\\B.mdf and did the same with ldf file. 在还原对话框中,我检查了覆盖现有数据库并将LogicalFileName从C:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLSERVER2012\\MSSQL\\DATA\\A.mdfC:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLSERVER2012\\MSSQL\\DATA\\B.mdf和ldf文件一样。 But I am getting 但我得到了

Exclusive access could not be obtained because the database is in use . Exclusive access could not be obtained because the database is in use

Also tried, 也尝试过,

ALTER DATABASE [B] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

Also sp_who2, there was no existing connection of [B] 也是sp_who2,没有[B]的现有连接

A cause for the attempt to get exclusive access comes from the options page of the restore dialog in SQL Server 2012 Management Studio. 尝试获取独占访问权的原因来自SQL Server 2012 Management Studio中的还原对话框的选项页面。 It will turn on tail-log and leave in restoring state options for the SOURCE database. 它将打开尾部日志并保留恢复SOURCE数据库的状态选项。 So, it will try to gain exclusive access to the source database (in this case A) in order to perform this action. 因此,它将尝试获得对源数据库的独占访问权(在本例中为A)以执行此操作。 If you turn off the tail log option, you will find that the operation works much more smoothly. 如果关闭尾部日志选项,您会发现操作更顺畅。

The answer was very simple, 答案很简单,

Run this command to grab the LogicalNames, 运行此命令以获取LogicalNames,

RESTORE FILELISTONLY FROM DISK = 'C:\Users\MyUSer\Desktop\A.bak'

Then Just put the in LogicalName in below, 然后只需将LogicalName放在下面,

RESTORE DATABASE B
   FROM DISK = 'C:\Users\MyUSer\Desktop\A.bak'
   WITH 
   MOVE 'LogicalName' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVER2012\MSSQL\Data\B.mdf', 
   MOVE 'LogicalName_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVER2012\MSSQL\Data\B.ldf'
GO

Note you might need to change the path. 请注意,您可能需要更改路径。 Helpful links, 有用的网址,

How to restore to a different database in sql server? 如何在sql server中恢复到不同的数据库?

http://technet.microsoft.com/en-us/library/ms186390.aspx http://technet.microsoft.com/en-us/library/ms186390.aspx

B. 'Restore Database' Dialog will be displayed on the General page 1. The name of the restoring database appears in the To database list box. B.“恢复数据库”对话框将显示在“常规”页面上1.恢复数据库的名称将显示在“到数据库”列表框中。 To create a new database, enter its name in the list box. 要创建新数据库,请在列表框中输入其名称。

  1. Select 'From device' 选择“来自设备”

  2. Click button to display 'Specify Backup' Dialog 单击按钮以显示“指定备份”对话框

  3. Click 'Add' to browse the .bak file from the directory and click OK 单击“添加”以从目录中浏览.bak文件,然后单击“确定”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM