下面是我創建的存儲過程,用於使用OLE自動化和ADO的Stream對象讀取離線.mdf。該解決方案更上下文張貼在這裏以及:https://social.technet.microsoft.com/Forums/sqlserver/en-US/3de5b574-0751-44a2-b69f-fa0c20378359/how-to-determine-sql-server-version-of-an-mdf-file?forum=sqlsetupandupgrade
CREATE /*ALTER*/ PROCEDURE spDBVersion
(
@FilePath nvarchar(500) /*Your filepath here EG 'C:\SQLData\somedatabase.mdf'*/
)
AS
DECLARE @HR int,
@objStream int,
@strErrorMessage Varchar(1000),
@Source nvarchar(255),
@Desc nvarchar(255),
@offset bigint,
@binary1 binary,
@binary2 binary
SET NOCOUNT ON
SELECT @strErrorMessage='Creating ADO stream object'
EXEC @HR = sp_OACreate 'ADODB.Stream' , @objStream OUT
IF @HR <> 0 GOTO CLEANUP
SELECT @strErrorMessage='Opening ADO stream'
EXEC @HR = sp_OAMethod @objStream, 'Open'
IF @HR <> 0 GOTO CLEANUP
SELECT @strErrorMessage='Declaring ADO stream type binary'
EXEC @HR = sp_OASetProperty @objStream, 'Type', 1
IF @HR <> 0 GOTO CLEANUP
SELECT @strErrorMessage='Opening File into ADO stream'
EXEC @HR = sp_OAMethod @objStream, 'LoadFromFile', NULL , @FilePath
IF @HR <> 0 GOTO CLEANUP
SELECT @offset = 73828 --9 * 8192 + 96 + 4
SELECT @strErrorMessage='Setting offset to read file'
EXEC @HR = sp_OASetProperty @objStream, 'Position', @offset
IF @HR <> 0 GOTO CLEANUP
SELECT @strErrorMessage = 'Reading Version number from mdf'
EXEC @HR = sp_OAMethod @objstream, 'Read', @binary1 OUT , 1
IF @HR <> 0 GOTO CLEANUP
SELECT @strErrorMessage = 'Reading Version number from mdf'
EXEC @HR = sp_OAMethod @objstream, 'Read', @binary2 OUT , 1
IF @HR <> 0 GOTO CLEANUP
SELECT @binary1 + 256 * @binary2 AS DatabaseVersion
END_ROUTINE:
RETURN
CLEANUP:
BEGIN
EXEC sp_OAGetErrorInfo @objStream,
@Source OUT,
@Desc OUT;
SELECT ErrorInSP='spDBVersion',
HR = convert(varbinary(4),@HR),
[email protected],
[email protected],
[email protected]
GOTO END_ROUTINE
END
GO
此存儲過程需要OLE自動化,所以你會希望把它包在一些重新配置語句假設OLE被禁用(因爲它可能應該是)。
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'Ole Automation Procedures', 1
RECONFIGURE
GO
exec spDBVersion 'E:\SQLData\somedatabase.mdf'
GO
EXEC sp_configure 'Ole Automation Procedures', 0
RECONFIGURE
GO
EXEC sp_configure 'show advanced options', 0
RECONFIGURE
GO
我似乎在將SQL Server 2008數據庫附加到SQL Server 2012實例時遇到問題。這是不同的情況嗎?這是促使我問這個問題的原因。我無法升級版本。降級甚至支持嗎? – J2Tuner 2013-04-23 14:34:59
對不起,我只是重新閱讀你的文章的最後一行。我仍然有問題將舊版本附加到較新的實例,雖然... – J2Tuner 2013-04-23 14:37:48
@ J2Tuner什麼是「問題」,你在說什麼「年長」?例如,你不能附加2000到2012,但是任何比2000年更新的東西都應該適用於你所擁有的最新版本。 – 2013-04-23 14:39:48