What are the possible ways to determine the deployed SQL Server version?
I’ve tried to do it using the SQL Server software. I want to do it using a command line SQL statement.
What are the possible ways to determine the deployed SQL Server version?
I’ve tried to do it using the SQL Server software. I want to do it using a command line SQL statement.
Gives 8.00, 9.00, 10.00 and 10.50 for SQL 2000, 2005, 2008 and 2008R2 respectively.
Also, Try the system extended procedure
xp_msver
. You can call this stored procedure likeif this value is 0 is not a express edition
Following are possible ways to see the version:
Method 1: Connect to the instance of SQL Server, and then run the following query:
An example of the output of this query is as follows:
Method 2: Connect to the server by using Object Explorer in SQL Server Management Studio. After Object Explorer is connected, it will show the version information in parentheses, together with the user name that is used to connect to the specific instance of SQL Server.
Method 3: Look at the first few lines of the Errorlog file for that instance. By default, the error log is located at Program Files\Microsoft SQL
Server\MSSQL.n\MSSQL\LOG\ERRORLOG
andERRORLOG.n
files. The entries may resemble the following:As you can see, this entry gives all the necessary information about the product, such as version, product level, 64-bit versus 32-bit, the edition of SQL Server, and the OS version on which SQL Server is running.
Method 4: Connect to the instance of SQL Server, and then run the following query:
Note This query works with any instance of SQL Server 2000 or of a later version
TL;DR
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 lineERRORLEVEL
if >= 16 (and the second line goes to the:ExitFail
label if the aforementionedERRORLEVEL
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 returnsNULL
).If you are interested in earlier versions you can use the
PARSENAME
function to split theProductVersion
(remembering the "parts" are numbered right to left i.e.PARSENAME('a.b.c', 1)
returnsc
).Also remember that
PARSENAME('a.b.c', 4)
returnsNULL
, because SQL 2005 and earlier only used 3 parts in the version number!So for SQL 2008+ you can simply use:
For SQL 2000-2005 you can use:
(the
PARSENAME(...,0)
is a hack to improve readability)So a check for a SQL 2000+ version would be:
This is a lot simpler if you're only only interested in SQL 2008+ because
SERVERPROPERTY('ProductMajorVersion')
returnsNULL
for earlier versions, so you can use:And you can use the
ProductLevel
andEdition
(orEngineEdition
) properties to determine RTM / SPn / CTPn and Dev / Std / Ent / etc respectively.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.