gpt4 book ai didi

sql - 如何从备份文件中检索数据库的逻辑文件名

转载 作者:行者123 更新时间:2023-12-01 19:16:21 25 4
gpt4 key购买 nike

我正在研究如何Restore Database Backup using SQL Script (T-SQL)的步骤。步骤如下:

Database YourDB has full backup YourBackUpFile.bak. It can be restored using following two steps:

Step 1: Retrieve the logical file name of the database from the backup.

RESTORE FILELISTONLY
FROM DISK = 'D:BackUpYourBackUpFile.bak'
GO

Step 2: Use the values in the LogicalName column in the following step.

----Make Database to single user Mode
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE

----Restore Database
RESTORE DATABASE YourDB
FROM DISK = 'D:BackUpYourBackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf'

我只是在如何获取 YourMDFLogicalNameYourLDFLogicalName 方面遇到问题。谁能帮我解决这个问题吗?

最佳答案

DECLARE @Table TABLE (LogicalName varchar(128),[PhysicalName] varchar(128), [Type] varchar, [FileGroupName] varchar(128), [Size] varchar(128), 
[MaxSize] varchar(128), [FileId]varchar(128), [CreateLSN]varchar(128), [DropLSN]varchar(128), [UniqueId]varchar(128), [ReadOnlyLSN]varchar(128), [ReadWriteLSN]varchar(128),
[BackupSizeInBytes]varchar(128), [SourceBlockSize]varchar(128), [FileGroupId]varchar(128), [LogGroupGUID]varchar(128), [DifferentialBaseLSN]varchar(128), [DifferentialBaseGUID]varchar(128), [IsReadOnly]varchar(128), [IsPresent]varchar(128), [TDEThumbprint]varchar(128)
)
DECLARE @Path varchar(1000)='C:\SomePath\Base.bak'
DECLARE @LogicalNameData varchar(128),@LogicalNameLog varchar(128)
INSERT INTO @table
EXEC('
RESTORE FILELISTONLY
FROM DISK=''' +@Path+ '''
')

SET @LogicalNameData=(SELECT LogicalName FROM @Table WHERE Type='D')
SET @LogicalNameLog=(SELECT LogicalName FROM @Table WHERE Type='L')

SELECT @LogicalNameData,@LogicalNameLog

更新

根据Microsoft site :

SQL Server files have two names:

logical_file_name

The logical_file_name is the name used to refer to the physical file in all Transact-SQL statements. The logical file name must comply with the rules for SQL Server identifiers and must be unique among logical file names in the database.

os_file_name

The os_file_name is the name of the physical file including the directory path. It must follow the rules for the operating system file names.

关于sql - 如何从备份文件中检索数据库的逻辑文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7089627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com