How to tell what version of SQLServer is installed

From NSIS Wiki
Jump to navigationJump to search
Author: icebrrrg (talk, contrib)


Description

I wrote a function to check what version of SQLServer was installed; I need SQL2kSP2 or later. This function relied on the StrTok function, which you'll also need in your script.

Usage

Usage goes a little something like this:

	Call CheckMinSQLVersion
	Pop $0
	IntCmp $0 1 +2
	Abort

The Function

;--------------------------------
; CheckMinSQLVersion
;
; Written by Matthew Kershaw (matthew.kershaw@us.didata.com)
;
; For this function the min SQL version must be SQL2kSP2 ...
; typically the SQLServer version is located in 
; HKLM\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\CurrentVersion
; but for SPs, the new version is located in
; HKLM\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\CSDVersion.
; For SP2, the value is 8.00.534. If the CSDVersion key does not exist, 
; no SP has been installed.
Function CheckMinSQLVersion
 
	ClearErrors
 
	ReadRegStr $0 HKLM "SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion" "CurrentVersion"
	IfErrors SQLServerNotFound SQLServerFound
 
	SQLServerFound:
		ReadRegStr $4 HKLM "SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion" "CSDVersion"
		IfErrors SPNotFound SPFound
 
	SPFound:
 
		;Check the first digit of the version; must be 8
		StrCpy $0 $4
		StrCpy $1 $0 1
		StrCmp $1 "8" SQLServer2000Found SQLServerVersionError
		Goto ExitCheckMinSQLVersion
 
	SQLServer2000Found:
 
		Push $0
		Push "."
		Call StrTok			;For an example string of "8.00.194", "8" and "00.194" now on stack
		Pop $2
		Push "."
		Call StrTok			;"00" and "194" now on stack
		Pop $2
		Pop $3
 
		IntCmp $3 534 0 SQLServerVersionError 0
		Push 1
		Goto ExitCheckMinSQLVersion
 
	SQLServerVersionError:
		MessageBox MB_OK|MB_ICONEXCLAMATION  "This product requires a minimum SQLServer version of 8.00.534 (SQLServer 2000, SP2); detected version $0. Setup will abort."
		Push 0
		Goto ExitCheckMinSQLVersion
 
	SQLServerNotFound:
		MessageBox MB_OK|MB_ICONEXCLAMATION  "SQLServer was not detected; this is required for installation. Setup will abort."
		Push 0
		Goto ExitCheckMinSQLVersion
 
	SPNotFound:
		MessageBox MB_OK|MB_ICONEXCLAMATION  "SQLServer version $0 was detected. SQLServer 2000 Service Pack 2 (or later) is required for installation. Setup will abort."
		Push 0
		Goto ExitCheckMinSQLVersion
 
	ExitCheckMinSQLVersion:
 
FunctionEnd