简体   繁体   English

从.bak文件还原时动态获取数据库逻辑名称

[英]Dynamically get DB logical name when restoring from .bak file

I am trying to create a python script that will dynamically restore databases from .bak files. 我正在尝试创建一个python脚本,该脚本将从.bak文件中动态还原数据库。 Running the query 运行查询

RESTORE DATABASE db_name FROM DISK = '\\path\to\db\db_name.bak' WITH REPLACE

works great if the original database name doesn't exist, however since there is multiple servers and often duplicate names this is not always the case. 如果原始数据库名称不存在,则效果很好,但是由于有多个服务器,并且名称经常重复,所以情况并非总是如此。

In the case that the .mdf name contained in the .bak file does exist, the WITH MOVE syntax is useful as shown below 如果确实存在.bak文件中包含的.mdf名称,则如下所示使用WITH MOVE语法

RESTORE DATABASE db_name 
  FROM DISK = '\\path\to\db\db_name.bak'
  WITH 
    MOVE 'Logical_name' TO '\\somepath\...\MyDB.mdf'

Unfortunately, this requires knowledge of the logical name before the query is run and is therefore not very useful for a general purpose script. 不幸的是,这要求在运行查询之前了解逻辑名称,因此对于通用脚本不是很有用。 Is there any way to get the logical name and populate it dynamically to make this more general purpose? 有没有什么方法可以获取逻辑名并动态填充它以使其更通用?

使用restore filelistonly逗号

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

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