我知道 OP 使用的是2008,但是我们都已经前进了几年,我刚刚为2014编写了一个存储过程,它选择了DatabaseBackupLSN
,所以我想我会分享...
CREATE PROCEDURE [Utilities].[GetDatabaseBackupLsn]
(
@filePath VARCHAR(1000),
@databaseBackupLsn NUMERIC(25, 0) OUT
)
AS
BEGIN
DECLARE @backupInfo TABLE
(
BackupName nvarchar(128),
BackupDescription nvarchar(255),
BackupType smallint,
ExpirationDate datetime,
Compressed bit,
Position smallint,
DeviceType tinyint,
UserName nvarchar(128),
ServerName nvarchar(128),
DatabaseName nvarchar(128),
DatabaseVersion int,
DatabaseCreationDate datetime,
BackupSize numeric(20, 0),
FirstLSN numeric(25, 0),
LastLSN numeric(25, 0),
CheckpointLSN numeric(25, 0),
DatabaseBackupLSN numeric(25, 0),
BackupStartDate datetime,
BackupFinishDate datetime,
SortOrder smallint,
[CodePage] smallint,
UnicodeLocaleId int,
UnicodeComparisonStyle int,
CompatibilityLevel tinyint,
SoftwareVendorId int,
SoftwareVersionMajor int,
SoftwareVersionMinor int,
SoftwareVersionBuild int,
MachineName nvarchar(128),
Flags int,
BindingId uniqueidentifier,
RecoveryForkId uniqueidentifier,
Collation nvarchar(128),
FamilyGUID uniqueidentifier,
HasBulkLoggedData bit,
IsSnapshot bit,
IsReadOnly bit,
IsSingleUser bit,
HasBackupChecksums bit,
IsDamaged bit,
BeginsLogChain bit,
HasIncompleteMetaData bit,
IsForceOffline bit,
IsCopyOnly bit,
FirstRecoveryForkID uniqueidentifier,
ForkPointLSN numeric(25, 0),
RecoveryModel nvarchar(60),
DifferentialBaseLSN numeric(25, 0),
DifferentialBaseGUID uniqueidentifier,
BackupTypeDescription nvarchar(60),
BackupSetGUID uniqueidentifier,
CompressedBackupSize bigint,
Containment tinyint,
KeyAlgorithm nvarchar(32),
EncryptorThumbprint varbinary(20),
EncryptorType nvarchar(32)
)
DECLARE @sql NVARCHAR(1100)
SET @sql = N'RESTORE HEADERONLY FROM DISK = ''' + @filePath + ''''
INSERT @backupInfo
EXEC(@sql)
SELECT @databaseBackupLSN = DatabaseBackupLSN
FROM @backupInfo
END
RESTORE HEADERONLY
文档在这里。
示例用法:
DECLARE @databaseBackupLsn NUMERIC(25, 0)
EXEC GetDatabaseBackupLsn
'd:\transfer\YourDatabaseBackup_2015_07_09_05_31_59.bak',
@databaseBackupLsn OUT
SELECT @databaseBackupLsn
2016 年 2 月 18 日更新:SQL Server 2014 Service Pack 1在输出中添加了 3 个新列RESTORE HEADERONLY
:KeyAlgorithm
EncryptorThumbprint
EncryptorType
. 我已将这些列添加到上述过程中。