Pages

Men

rh

4/28/2013

Upgrading SQL Server databases and changing compatibility levels

Problem
When upgrading databases from an older version of SQL Server using either the 
backup and restore method or detach and attach method the compatibility level does not automatically change and therefore your databases still act as though they are running using an earlier version of SQL Server.  From an overall standpoint this is not a major problem, but there are certain features that you will not be able to take advantage of unless your database compatibly level is changed.  This tip will show you how to check the current compatibly level, how to change the compatibly level and also some of the differences between earlier versions and SQL Server 2005.

SolutionThe first thing that you need to do is to check the compatibility level that your database is running under.  As mentioned above any database that is upgraded using the backup and restore or detach and attach method will not change the compatibly level automatically, so you will need to check each database and make the change.
Although SQL Server has changed its naming convention to SQL Server 2000, 2005 and soon to be released 2008 the internal version numbers still remain.  Here is a list of the compatibly levels (versions) that you will see:
  • 60 = SQL Server 6.0
  • 65 = SQL Server 6.5
  • 70 = SQL Server 7.0
  • 80 = SQL Server 2000
  • 90 = SQL Server 2005

Identifying Compatibly Level
To check the compatibility level of your databases you can use one of these methods:
Using SQL Server Management Studio, right click on the database, select "Properties" and look at the "Options" page for each database as the following image shows:


Another option is to use sp_helpdb so you can get the information for all databases at once:
EXEC sp_helpdb

Or select directly from the sys.databases catalog to get the information for all databases.
SELECT * FROM sys.databases


Compatibly Level for New Databases
When issuing a CREATE DATABASE statement there is not a way to select which compatibility level you want to use.  The compatibility level that is used is the compatibility level of your model database.

Here is a sample CREATE DATABASE command, but there is not an option to change the compatibility level.
CREATE DATABASE [test] ON PRIMARY 
( NAME = N'test', FILENAME = N'Z:\SQLData\test.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB )
LOG ON 
( NAME = N'test_log', FILENAME = N'Y:\SQLData\test3_log.ldf' , SIZE = 3072KB , FILEGROWTH = 10%)
GO

When creating a database using SQL Server Management Studio you have the ability to change the compatibility level on the "Options" tab such as follows:


If we use the "Script" option we can see that SQL Server issues the CREATE DATABASE statement and then issues "sp_dbcmptlevel" to set the database compatibility level to 80 as shown below.

CREATE DATABASE [test] ON PRIMARY 
( NAME = N'test', FILENAME = N'Z:\SQLData\test.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB )
LOG ON 
( NAME = N'test_log', FILENAME = N'Y:\SQLData\test3_log.ldf' , SIZE = 3072KB , FILEGROWTH = 10%)
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'test', @new_cmptlevel=80
GO

Changing Compatibility Level
So once you have identified the compatibility level of your database and know what you want to change it to you can use the sp_dbcmptlevel system stored procedure to make the change.  The command has the following syntax:

sp_dbcmptlevel [ [ @dbname = ] name ] 
[ , [ @new_cmptlevel = ] version ]
--to change to level 80
dbo.sp_dbcmptlevel @dbname=N'test', @new_cmptlevel=80

--to change to level 90
dbo.sp_dbcmptlevel @dbname=N'test', @new_cmptlevel=90

--or
sp_dbcmptlevel 'test', '80'

sp_dbcmptlevel 'test', '90'

Differences
There are several differences on how compatibly levels affect your database operations.  SQL Server Books Online has a list of these differences and the following list shows you a few of these items:

Compatibility level setting of 80 or earlierCompatibility level setting of 90Possibility of impact
For locking hints in the FROM clause, the WITH keyword is always optional.With some exceptions, table hints are supported in the FROM clause only when the hints are specified with the WITH keyword. For more information, see FROM (Transact-SQL).High
The *= and =* operators for outer join are supported with a warning message.These operators are not supported; the OUTER JOIN keyword should be used.High
SET XACT_ABORT OFF is allowed inside a trigger.SET XACT_ABORT OFF is not allowed inside a trigger.Medium

(Source: SQL Server 2005 Books Online)  For a complete list of these items look here:
In addition, each new compatibility level offers a new list of reserved keywords.  Here is a list of the new keywords for SQL Server 2005.

Compatibility level settingReserved keywords
90PIVOT, UNPIVOT, REVERT, TABLESAMPLE
80COLLATE, FUNCTION, OPENXML
70BACKUP, CONTAINS, CONTAINSTABLE, DENY, FREETEXT, FREETEXTTABLE, PERCENT, RESTORE, ROWGUIDCOL, TOP
65AUTHORIZATION, CASCADE, CROSS, DISTRIBUTED, ESCAPE, FULL, INNER, JOIN, LEFT, OUTER, PRIVILEGES, RESTRICT, RIGHT, SCHEMA, WORK

(Source: SQL Server 2005 Books Online)
If one of these keywords is being used and your database is set to this compatibly level the commands will fail.  To get around this you could put the keyword in either square brackets ([ ]) or use quotation marks (" ") such as [PIVOT] or "PIVOT".


Summary
The compatibly level setting is used by SQL Server to determine how certain new features should be handled.  This was setup so you could migrate your databases to a later release of SQL Server without having to worry about the application breaking.  This setting can be changed forward and backwards if needed, so if you do change your compatibly level and find that there are problems you can set the value back again until you resolve all of the issues that you may be facing during the upgrade.

In addition, there are certain features that only work if the database is set to the latest compatibly level, therefore to get all of the benefits of the version of SQL Server you are running you need to make sure you are using the latest compatibly level.

 Source Collcted from MSSQLTIPS.COM

7 comments :

  1. Many people want to increase their muscle mass and create a toned,
    developed body that is not only aesthetically pleasing, but strong
    and healthy as well. To find out if it is actually
    true and to understand better if Kyle Leon's system is generally for you or not, let's
    look into a few of the benefits and drawbacks of the product.
    Yes, obviously, it is the most affordable way of buying protein since all you want to do is to obtain those egg whites
    with a tray of offspring.

    Feel free to visit my site; Muscle Maximizer Reviews

    ReplyDelete
  2. I known as all of the business in addition to actually spoken with the head honcho.
    Its hallmarks are the unrivaled tracking and segmentation secures and filters
    together with continuous product development.
    You can also apply this calculation to ezines subscribers.


    Also visit my weblog - CPA Power Blast Reviews

    ReplyDelete
  3. Imagine my surprise, when searching local information for live
    events, to see them playing in Calgary that very week, at The SODA on 17th Ave.
    Lauren: What's it like to live with the titles 'the King' or 'Prince of trance'. Most of them are my friends or family like my brother.

    My web-site: Jean Michel Jarre Discography Free Download

    ReplyDelete
  4. Somanabolic Muscle Maximizer program is optimized for the
    U. The 62 page Somanabolic Weight Training program that
    details the specific workouts you need to do depending on your somatotype (body
    type) in order to pack on lean muscle mass. Then opt
    for ones cheapest way to help you feed those guns while
    using the Egg White peptids.

    Check out my webpage ... Somanabolic Muscle Maximizer Reviews

    ReplyDelete
  5. The program comes with full warranty 60 days money back,
    so everyone can actually try this muscle building system for
    a few weeks and see the results without any risk
    at all. Yes we would like muscle, but in the right
    way, and gaining lean muscle without fat is the key
    to doing this. Who is this Somanabolic Muscle Maximizer Training Software Designed For.


    Feel free to surf to my webpage - Muscle Maximizer Reviews

    ReplyDelete
  6. Setting up a blog is not all that complicated and it is perfectly fine to use one of the free blogging platforms such as Blogger, Wordpress, or
    many more. For FREE weekly articles go to Sylvia Browder's Blog for Women Entrepreneurs, She can be contacted at info@nawomenrise. I usually come out and build 2-3 niche sites every week to target profitable micro niches.

    Here is my weblog exit cash machine plugin

    ReplyDelete
  7. Since we are on the topic of pinging, this is yet another reason given by Wordpress
    users of why they prefer this blogging platform.
    Secondly it truly is entirely absolutely free to make use
    of. Word - Press is an open source blog publishing application and can be
    used for basic content management.

    My site WP Social Press Review

    ReplyDelete