TL;DR
SQLCMD -S (LOCAL) -E -V 16 -Q "IF(ISNULL(CAST(SERVERPROPERTY('ProductMajorVersion') AS INT),0)<11) RAISERROR('You need SQL 2012 or later!',16,1)"
IF ERRORLEVEL 1 GOTO :ExitFail
This uses SQLCMD (comes with SQL Server) to connect to the local server instance using Windows auth, throw an error if a version check fails and return the @@ERROR
as the command line ERRORLEVEL
if >= 16 (and the second line goes to the :ExitFail
label if the aforementioned ERRORLEVEL
is >= 1).
Watchas, Gotchas & More Info
For SQL 2000+ you can use the SERVERPROPERTY to determine a lot of this info.
While SQL 2008+ supports the ProductMajorVersion
& ProductMinorVersion
properties, ProductVersion
has been around since 2000 (remembering that if a property is not supported the function returns NULL
).
If you are interested in earlier versions you can use the PARSENAME
function to split the ProductVersion
(remembering the "parts" are numbered right to left i.e. PARSENAME('a.b.c', 1)
returns c
).
Also remember that PARSENAME('a.b.c', 4)
returns NULL
, because SQL 2005 and earlier only used 3 parts in the version number!
So for SQL 2008+ you can simply use:
SELECT
SERVERPROPERTY('ProductVersion') AS ProductVersion,
CAST(SERVERPROPERTY('ProductMajorVersion') AS INT) AS ProductMajorVersion,
CAST(SERVERPROPERTY ('ProductMinorVersion') AS INT) AS ProductMinorVersion;
For SQL 2000-2005 you can use:
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;
(the PARSENAME(...,0)
is a hack to improve readability)
So a check for a SQL 2000+ version would be:
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);
This is a lot simpler if you're only only interested in SQL 2008+ because SERVERPROPERTY('ProductMajorVersion')
returns NULL
for earlier versions, so you can use:
IF (ISNULL(CAST(SERVERPROPERTY('ProductMajorVersion') AS INT), 0) < 11) -- SQL2012
RAISERROR('You need SQL 2012 or later!', 16, 1);
And you can use the ProductLevel
and Edition
(or EngineEdition
) properties to determine RTM / SPn / CTPn and Dev / Std / Ent / etc respectively.
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;
FYI the major SQL version numbers are:
And this all works for SQL Azure too!
EDITED: You may also want to check your DB compatibility level since it could be set to a lower compatibility.
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)