Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64) Mar 29 2009
10:11:52 Copyright (c) 1988-2008 Microsoft Corporation Express
Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: )
方法3: 查看该实例 Errorlog 文件的前几行。默认情况下,错误日志位于 Program Files MicrosoftSQLServer\MSSQL.n\MSSQL\LOG\ERRORLOG和 ERRORLOG.n文件中。条目可能类似于下列内容:
2011-03-27 22:31:33.50 Server Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64) Mar 29 2009 10:11:52 Copyright (c) 1988-2008 Microsoft Corporation Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: )
如您所见,此条目提供了有关产品的所有必要信息,例如版本、产品级别、64位与32位、 SQL Server 的版本以及运行 SQL Server 的操作系统版本。
SELECT
SERVERPROPERTY('ProductVersion') AS ProductVersion,
CAST(SERVERPROPERTY('ProductMajorVersion') AS INT) AS ProductMajorVersion,
CAST(SERVERPROPERTY ('ProductMinorVersion') AS INT) AS ProductMinorVersion;
对于 SQL2000-2005,您可以使用:
SELECT
SERVERPROPERTY('ProductVersion') AS ProductVersion,
CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 3 ELSE 4 END) AS INT) AS ProductVersion_Major,
CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 2 ELSE 3 END) AS INT) AS ProductVersion_Minor,
CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 1 ELSE 2 END) AS INT) AS ProductVersion_Revision,
CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 0 ELSE 1 END) AS INT) AS ProductVersion_Build;
(PARSENAME(...,0)是一种提高可读性的技巧)
因此,对 SQL 2000 + 版本的检查应该是:
IF (CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 3 ELSE 4 END) AS INT) < 10) -- SQL2008
OR (
(CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 3 ELSE 4 END) AS INT) = 10) -- SQL2008
AND (CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME), CASE WHEN SERVERPROPERTY('ProductVersion') IS NULL THEN 2 ELSE 1 END) AS INT) < 5) -- R2 (this may need to be 50)
)
RAISERROR('You need SQL 2008R2 or later!', 16, 1);
SELECT
CAST(SERVERPROPERTY('ProductVersion') AS SYSNAME) AS ProductVersion,
CAST(SERVERPROPERTY('ProductLevel') AS SYSNAME) AS ProductLevel,
CAST(SERVERPROPERTY('Edition') AS SYSNAME) AS Edition,
CAST(SERVERPROPERTY('EngineEdition') AS INT) AS EngineEdition;
IF EXISTS (SELECT * FROM sys.databases WHERE database_id=DB_ID() AND [compatibility_level] < 110)
RAISERROR('Database compatibility level must be SQL2008R2 or later (110)!', 16, 1)
Microsoft SQL Server 2012 - 11.0.2100.60 (X64)
Feb 10 2012 19:39:15
Copyright (c) Microsoft Corporation
Express Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
SELECT
@@servername AS 'Server Name'
,CONNECTIONPROPERTY('local_net_address') AS [IP Address]
,d.name AS [Database_Name]
,d.compatibility_level
,@@version AS 'Version'
,CASE
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '8%' THEN 'SQL2000'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '9%' THEN 'SQL2005'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '10.0%' THEN 'SQL2008'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '10.5%' THEN 'SQL2008 R2'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '11%' THEN 'SQL2012'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '12%' THEN 'SQL2014'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '13%' THEN 'SQL2016'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '14%' THEN 'SQL2017'
WHEN CONVERT(VARCHAR(128), SERVERPROPERTY('productversion')) LIKE '15%' THEN 'SQL2019'
ELSE 'unknown'
END AS SQL_Server_Version,
d.collation_name
,(SUM(CAST(mf.size AS BIGINT)) * 8 / 1024) / 1024 AS Size_GBs
FROM sys.master_files mf
INNER JOIN sys.databases d
ON d.database_id = mf.database_id
WHERE d.database_id > 4 -- Skip system databases
GROUP BY d.name
,d.compatibility_level
,d.collation_name
ORDER BY d.name