Wednesday 31 December 2014

SQL Migration from 2008 to 2012

The project that I work at the present time has received the new SQL Server hardware,
So now is the time to upgrade from the SQL server 2008 to the 2012.

An upgrade, in new environment, refers to the process of moving from the SQL server version 2008 to the new version 2012.

There are two approaches for upgrading database:
  1. In position: The SQL Server is upgraded where it is currently installed
  2. Migration: A new environment is installed; the data is copied to it and configured with the existing data.
The approach which I have taken is the second one, since I had a new server and I am going to do a fresh SQL Server 2012 installation.

The upgrade of the databases can be implemented in the following steps:
  1. Backup the SQL Server 2008 databases and restored them in SQL 2012.
  2. Change each database compatibility level from 2008 to 2012 and update statistics.

ALTER DATABASE Database SET COMPATIBILITY_LEVEL = 110;

DECLARE @sql NVARCHAR(MAX)
set @sql=  N'';
SELECT @sql = CHAR(13) + CHAR(10) + 'UPDATE STATISTICS '
  + QUOTENAME(SCHEMA_NAME(schema_id))
  + '.' + QUOTENAME(name) + ' WITH FULLSCAN;'
  FROM sys.tables;

PRINT @sql;


EXEC sp_executesql @sql;

where [database] is the database to change the compatibility level

We can do it in a more compact way with

EXEC sp_MSforeachtable 'UPDATE STATISTICS ? WITH FULLSCAN'

or go to the database properties and on the options select the Compatibility Level 110.

  3. Check the logical and physical integrity of all the objects in the upgraded databases: 

DBCC CHECKDB([database]) WITH NO_INFOMSGS

Where [database] is the database to run the integrity checks NO_INFOMSGS option suppresses all informational messages.

If DBCC printed any error messages, we must fix them so that your database will work correctly.

The following script can be useful to check database compatibility level:

select name, compatibility_level , version_name =
CASE compatibility_level
    WHEN 65  THEN 'SQL Server 6.5'
    WHEN 70  THEN 'SQL Server 7.0'
    WHEN 80  THEN 'SQL Server 2000'
    WHEN 90  THEN 'SQL Server 2005'
    WHEN 100 THEN 'SQL Server 2008/R2'
    WHEN 110 THEN 'SQL Server 2012'
    WHEN 120 THEN 'SQL Server 2014'
END

from sys.databases order by compatibility_level

About Author:
Raghwendra Mishra is DBA who works as associate consultant with Systems Plus Pvt. Ltd. He in free time reads and write on various web technologies. He can be contacted at: raghwendra.mishra@spluspl.com

3 comments:

  1. Your blog is very informative and I have learnt lot of things. Ink Online used for all types of hp printers.

    ReplyDelete
  2. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    erp solution providers

    ReplyDelete