There are different ways to get the database internal versions in SQL Server. Following are the different ways:
DATABASEPROPERTYEX:
Returns the current setting of the specified database option or property for the specified database.
Syntax:
DATABASEPROPERTYEX (database, property)
where
database: Is an expression that contains the name of the database for which to return the named property information. database is nvarchar(128).
property: Is an expression that contains the name of the database property to return. property is varchar(128), and can be one of the following values.
In the database parameter, we can pass database that we are interested to find the database internal version.
Property is the version, there are many properties for the database but we are interested to get the version of the database so we can pass the version here.
SELECT DATABASEPROPERTYEX('SQLQUIZ2011', 'version')
DATABASEPROPERTY:
DATABASEPROPERTY returns the named database property value for the specified database and property name. This feature will be removed in the next version of SQL Server.
Syntax:
DATABASEPROPERTY (database , property )
where
database: Is an expression that contains the name of the database for which to return the named property information. database is nvarchar(128).
property: Is an expression that contains the name of the database property to return. property is varchar(128), and can be one of the following values. Property is the version, there are many properties for the database but we are interested to get the version of the database so we can pass the version here.
Using sys.sysdatabases:
Contains one row for each database in an instance of Microsoft SQL Server. When SQL Server is first installed, sysdatabases contains entries for the master, model, msdb, and tempdb databases.
select version, name from sys.sysdatabases where name = ' SQLQUIZ2011'
DBCC Commands:
Using DBCC DBINFO
DBCC DBCC TRACEON (3604);
GO
DBCC DBINFO
GO
DBCC TRACEOFF (3604);
The output The dbiversion shows the current version of the database. You can also tell if the database was created in a previous version of SQL Server with the dbicreateVersion.
Following is the output of DBCC DBINFO command:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBINFO STRUCTURE:
DBINFO @0x0ADEE3E8
dbi_dbid = 11 dbi_status = 65536 dbi_nextid = 2121058592
dbi_dbname = SQLQUIZ2011 dbi_maxDbTimestamp = 2000 dbi_version = 655
dbi_createVersion = 655 dbi_ESVersion = 0
dbi_nextseqnum = 1900-01-01 00:00:00.000 dbi_crdate = 2011-10-21 18:50:28.040
dbi_filegeneration = 0
dbi_checkptLSN
m_fSeqNo = 37 m_blockOffset = 409 m_slotId = 1
dbi_RebuildLogs = 0 dbi_dbccFlags = 2
dbi_dbccLastKnownGood = 1900-01-01 00:00:00.000
dbi_dbbackupLSN
Using DBCC Page command:
DBCC TRACEON (3604);
GO
DBCC PAGE ('SQLQUIZ2011', 1, 9, 3);
GO
DBCC TRACEOFF (3604);
Restoring database from earlier version of SQL Server to a newer version of SQL Server, we will be able to see the version number in the error log.
If we want to get the internal version information of all the database from the SQL Server instance on the server we can use the following code from the Quiz Master excellent blog.
.-----Script to find DB version for all the databases.
CREATE TABLE #version (
VersionID INT
, VersionName VARCHAR (255)
)
INSERT #version
SELECT 515, 'SQL Server 7.0' UNION ALL
SELECT 539, 'SQL Server 2000' UNION ALL
SELECT 611, 'SQL Server 2005, VARDECIMAL disabled' UNION ALL
SELECT 612, 'SQL Server 2005, VARDECIMAL enabled' UNION ALL
SELECT 655, 'SQL Server 2008' UNION ALL
SELECT 660, 'SQL Server 2008 R2'
CREATE TABLE #DBCC (
ParentObject VARCHAR (255)
, [Object] VARCHAR (255)
, Field VARCHAR (255)
, [Value] VARCHAR (255)
)
CREATE TABLE #DBCCResults (
ServerName VARCHAR (255)
, DBName VARCHAR (255)
, VersionCreated INT
, CurrentVersion INT
, CreatedDate DATETIME
)
EXEC master.dbo.SP_MSFOREACHDB
@Command1 = 'USE ? INSERT INTO #DBCC EXECUTE (''DBCC DBINFO WITH TABLERESULTS'')'
, @Command2 = 'INSERT INTO #DBCCResults (ServerName, DBName, VersionCreated, CurrentVersion, CreatedDate)
SELECT TOP 1 @@SERVERNAME
, ''?''
, (SELECT Value FROM #DBCC WHERE Field = ''dbi_createVersion'')
, (SELECT Value FROM #DBCC WHERE Field = ''dbi_version'')
, (SELECT Value FROM #DBCC WHERE Field = ''dbi_crdate'')
FROM #DBCC'
, @Command3 = 'TRUNCATE TABLE #DBCC'
SELECT ServerName
, DBName
, v1.VersionName AS VersionCreated
, v2.VersionName AS CurrentVersion
, CreatedDate
FROM #DBCCResults dr JOIN #version v1
ON dr.VersionCreated = v1.VersionID
JOIN #version v2
ON dr.CurrentVersion = v2.VersionID
DROP TABLE #version, #DBCC, #DBCCRESULTS
Output of the above query looks as shown below and we can get the database version from the CurrentVersion column.
Fig 1: Output of the Above Query showing all database versions on the server.
This is infact a nice question that helped to get the idea of how to get the database internal versions using different options.
http://sankarreddy.com/2010/05/database-internal-version-create-version-and-current-version/