Due to the widespread use of SQLServer2000, many companies also want to use the new SQL Server to directly "detach/Attach" or "Backup/restore" the database to be stored between different versions. The issue of incompatible versions is often encountered. A few days ago I met a database backed up from my native 2008r2. Restore to 2008 above times wrong:
This version incompatibility problem can be seen from running version 10.50.2500 (2008r2 is 10.50) and 10.00.1600 (2008 is 10.00), in most cases upgrading from a lower version to a higher version, as long as the span is not too large, such as 2000 upgrade to 2012, there will be no error. Unless some new incompatible features such as *= are used to implement a LEFT JOIN statement. But like that, when you restore from a high version to a lower version, the problem arises and almost certainly makes an error.
Here are a few tips, with the example being downgraded from 2008 to 2005:
Method One: Use the graphical operation (GUI) to open ssms (SQL Server Management Studio)
Step 1: Right-click the database you want to downgrade, press Select:
Step 2: In the dialog box, select:
Step 3: Select in "Advanced":
Step 4: Save the script and run the script in SQLServer2005. Detailed steps can be seen: http://bbs.csdn.net/topics/390438560?page=1#post-394316973 in the 13 floor of the reply, there is step 5: Through the "task" → "Import data", Importing the data from 2008 to a library created using the script is done as follows:
Method Two: Using the system's own stored procedure implementation:sp_dbcmptlevel--Set Some database behavior to be compatible with the specified version of SQL Server
Here is its internal implementation code:
[SQL]View PlainCopyprint?
- SET quoted_identifier on
- SET ansi_nulls on
- GO
- Create procedure sys.sp_dbcmptlevel --1997/04/15
- @dbname sysname = NULL, --database name to change
- @new_cmptlevel tinyint = NULL OUTPUT -The new compatibility level
- as
- SET NOCOUNT on
- declare @exec_stmt nvarchar (max)
- declare @returncode int
- declare @comptlevel float (8)
- declare @dbid int --dbid of the database
- DECLARE @dbsid varbinary --ID of the owner of the database
- declare @orig_cmptlevel tinyint --Original compatibility level
- declare @input_cmptlevel tinyint --compatibility level passed in by user
- , @cmptlvl80 tinyint --compatibility to SQL Server Version 8.0
- , @cmptlvl90 tinyint --compatibility to SQL Server Version 9.0
- , @cmptlvl100 tinyint --compatibility to SQL Server Version 10.0
- Select @cmptlvl80 =
- @cmptlvl90 = 90,
- @cmptlvl100 = 100
- --SP must is called at adhoc level- -
- if (@ @nestlevel > 1)
- begin
- RAISERROR (15432,-1,-1,' sys.sp_dbcmptlevel ')
- return (1)
- End
- --If no @dbname given, just list the valid compatibility level values.
- If @dbname is null
- begin
- RAISERROR (15048,-1,-1, @cmptlvl80, @cmptlvl90, @cmptlvl100)
- return (0)
- End
- --Verify The database name and get info
- Select @dbid = dbid, @dbsid = sid, @orig_cmptlevel = Cmptlevel
- From master.dbo.sysdatabases
- where name = @dbname
- --If @dbname not found, say so and list the databases.
- If @dbid is null
- begin
- RAISERROR (15010,-1,-1, @dbname)
- Print '
- Select name as ' Available databases: '
- From master.dbo.sysdatabases
- return (1)
- End
- --now save the input compatibility level and initialize the return clevel
- -- to is the current Clevel
- Select @input_cmptlevel = @new_cmptlevel
- Select @new_cmptlevel = @orig_cmptlevel
- --If no clevel is supplied, display and output current level.
- If @input_cmptlevel is null
- begin
- RAISERROR (15054,-1,-1, @orig_cmptlevel)
- return (0)
- End
- --If invalid clevel given, print usage and return error code
- --' usage:sp_dbcmptlevel [dbname [, CompatibilityLevel]] '
- If @input_cmptlevel not in (@cmptlvl80, @cmptlvl90, @cmptlvl100)
- begin
- RAISERROR (15416,-1,-1)
- Print '
- RAISERROR (15048,-1,-1, @cmptlvl80, @cmptlvl90, @cmptlvl100)
- return (1)
- End
- --only the SA or the dbo of @dbname can execute the update part
- --of this procedure sys.so check.
- if (Not(is_srvrolemember (' sysadmin ') = 1)) and Suser_sid () <> @dbsid
- --ALSO allow db_owner only IF DB requested are current db
- and (@dbid <> db_id () or is_member (' db_owner ') <> 1)
- begin
- RAISERROR (15418,-1,-1)
- return (1)
- End
- --If we ' re in a transaction, disallow this since it might make recovery impossible.
- set implicit_transactions off
- If @ @trancount > 0
- begin
- RAISERROR (15002,-1,-1,' sys.sp_dbcmptlevel ')
- return (1)
- End
- Set @exec_stmt = ' ALTER DATABASE ' + QuoteName (@dbname, ' [') + ' Set compatibility_level = ' + cast (@i Nput_cmptlevel as nvarchar (+))
- --note:database @dbname may not exist anymore
- exec (@exec_stmt)
- Select @new_cmptlevel = @input_cmptlevel
- return (0) --sp_dbcmptlevel
- GO
Grammar
[SQL]View PlainCopy print?
- sp_dbcmptlevel [[@dbname =] name]
- [, [@new_cmptlevel =] version]
Parameters
-
[
@dbname = ]
name
-
The name of the database for which you want to change the compatibility level. Database names must conform to the rules for identifiers. The data type of name is sysname, and the default value is NULL.
-
[
@new_cmptlevel = ]
version
-
The version of SQL Server to which the database is compatible. The data type of version is tinyint, and the default value is NULL. The value must be one of the following values:
SQL Server 2000
2005 = SQL Server
+ = SQL Server 2008
Return code value
0 (Success) or 1 (failed)
Caveats: Subsequent versions of Microsoft SQL Server will remove the feature. Do not use this feature in new development work, and modify applications that are currently using the feature as soon as possible. Use the ALTER DATABASE compatibility level instead.
How do I downgrade a SQL Server database from a higher version to a lower version?