如何把SQLServer資料庫從高版本降級到低版本?

來源:互聯網
上載者:User

標籤:

由於目前還廣泛使用著SQLServer2000,很多公司又想使用新的SQLServer,從而直接【分離/附加】或者【備份/還原】資料庫,在不同版本之間存放。往往就會遇到版本不相容的問題。前幾天遇到了從我本機2008R2上備份的一個資料庫還原到2008上面時報錯:

從運行版本10.50.2500(2008R2是10.50)和10.00.1600(2008是10.00)中可以看出這個版本不相容問題,大部分情況下,從低版本升級到高版本,只要不是跨度太大,如2000升級到2012,都不會怎麼報錯。除非使用了一些新版本不相容的特性如*=來實現left join的語句。但是就像那樣,從高版本還原到低版本的時候,問題就出現了,而且幾乎一定會報錯。

下面給出幾個小建議,例子是從2008 降級到2005:

方法一:使用圖形化操作(GUI),開啟SSMS(SQL Server Management Studio)

 

步驟1:右鍵你要降級的資料庫,按選擇:

 

步驟2:在對話方塊中選擇:

   步驟3:在【進階】中選擇:

 

步驟4:把指令碼儲存起來,然後在SQLServer2005中運行指令碼。詳細步驟可以看:http://bbs.csdn.net/topics/390438560?page=1#post-394316973 中的13樓的回複,有步驟5:通過【任務】→【匯入資料】,把資料從2008匯入到使用指令碼建立的庫上如,就完成了:

 

 

方法二:使用系統內建的預存程序實現:sp_dbcmptlevel ——將某些資料庫行為設定為與指定的 SQL Server 版本相容

下面是其內部實現代碼:

 

[sql] view plaincopyprint? 
  1. SET QUOTED_IDENTIFIER ON  
  2.  SET ANSI_NULLS ON  
  3.  GO  
  4.  create procedure sys.sp_dbcmptlevel            -- 1997/04/15  
  5.     @dbname sysname = NULL,                 -- database name to change  
  6.     @new_cmptlevel tinyint = NULL OUTPUT    -- the new compatibility level to change to  
  7.  as  
  8.     set nocount    on  
  9.    
  10.     declare @exec_stmt nvarchar(max)  
  11.     declare @returncode int  
  12.     declare @comptlevel float(8)  
  13.     declare @dbid int                   -- dbid of the database  
  14.     declare @dbsid varbinary(85)        -- id of the owner of the database  
  15.     declare @orig_cmptlevel tinyint     -- original compatibility level  
  16.     declare @input_cmptlevel tinyint    -- compatibility level passed in by user  
  17.         ,@cmptlvl80 tinyint             -- compatibility to SQL Server Version 8.0  
  18.         ,@cmptlvl90 tinyint             -- compatibility to SQL Server Version 9.0  
  19.         ,@cmptlvl100 tinyint                -- compatibility to SQL Server Version 10.0  
  20.     select  @cmptlvl80 = 80,  
  21.             @cmptlvl90 = 90,  
  22.             @cmptlvl100 = 100  
  23.    
  24.     -- SP MUST BE CALLED AT ADHOC LEVEL --  
  25.     if (@@nestlevel > 1)  
  26.     begin  
  27.         raiserror(15432,-1,-1,‘sys.sp_dbcmptlevel‘)  
  28.         return (1)  
  29.     end  
  30.    
  31.     -- If no @dbname given, just list the valid compatibility level values.  
  32.     if @dbname is null  
  33.     begin  
  34.        raiserror (15048, -1, -1, @cmptlvl80, @cmptlvl90, @cmptlvl100)  
  35.        return (0)  
  36.     end  
  37.    
  38.     --  Verify the database name and get info  
  39.     select @dbid = dbid, @dbsid = sid ,@orig_cmptlevel = cmptlevel  
  40.         from master.dbo.sysdatabases  
  41.         where name = @dbname  
  42.    
  43.     --  If @dbname not found, say so and list the databases.  
  44.     if @dbid is null  
  45.     begin  
  46.         raiserror(15010,-1,-1,@dbname)  
  47.         print ‘ ‘  
  48.         select name as ‘Available databases:‘  
  49.             from master.dbo.sysdatabases  
  50.         return (1)  
  51.     end  
  52.    
  53.     -- Now save the input compatibility level and initialize the return clevel  
  54.     -- to be the current clevel  
  55.     select @input_cmptlevel = @new_cmptlevel  
  56.     select @new_cmptlevel = @orig_cmptlevel  
  57.    
  58.     -- If no clevel was supplied, display and output current level.  
  59.     if @input_cmptlevel is null  
  60.     begin  
  61.         raiserror(15054, -1, -1, @orig_cmptlevel)  
  62.         return(0)  
  63.     end  
  64.    
  65.     -- If invalid clevel given, print usage and return error code  
  66.     -- ‘usage: sp_dbcmptlevel [dbname [, compatibilitylevel]]‘  
  67.     if @input_cmptlevel not in (@cmptlvl80, @cmptlvl90, @cmptlvl100)  
  68.     begin  
  69.         raiserror(15416, -1, -1)  
  70.         print ‘ ‘  
  71.         raiserror (15048, -1, -1, @cmptlvl80, @cmptlvl90, @cmptlvl100)  
  72.         return (1)  
  73.     end  
  74.    
  75.     --  Only the SA or the dbo of @dbname can execute the update part  
  76.     --  of this procedure sys.so check.  
  77.     if (not (is_srvrolemember(‘sysadmin‘) = 1)) and suser_sid() <> @dbsid  
  78.         -- ALSO ALLOW db_owner ONLY IF DB REQUESTED IS CURRENT DB  
  79.         and (@dbid <> db_id() or is_member(‘db_owner‘) <> 1)  
  80.     begin  
  81.         raiserror(15418,-1,-1)  
  82.         return (1)  
  83.     end  
  84.    
  85.     -- If we‘re in a transaction, disallow this since it might make recovery impossible.  
  86.     set implicit_transactions off  
  87.     if @@trancount > 0  
  88.     begin  
  89.         raiserror(15002,-1,-1,‘sys.sp_dbcmptlevel‘)  
  90.         return (1)  
  91.     end  
  92.    
  93.     set @exec_stmt = ‘ALTER DATABASE ‘ + quotename(@dbname, ‘[‘) + ‘ SET COMPATIBILITY_LEVEL = ‘ + cast(@input_cmptlevel as nvarchar(128))  
  94.    
  95.     -- Note: database @dbname may not exist anymore  
  96.     exec(@exec_stmt)  
  97.    
  98.     select @new_cmptlevel = @input_cmptlevel  
  99.    
  100.     return (0) -- sp_dbcmptlevel  
  101.  GO  
  102.    

 

文法 [sql] view plaincopyprint? 
  1. sp_dbcmptlevel [ [ @dbname = ] name ]   
  2.      [ , [ @new_cmptlevel = ] version ]  
  3.    


 參數 
@dbname = ]  name

要為其更改相容層級的資料庫的名稱。資料庫名稱必須符合標識符的規則。name 的資料類型為 sysname,預設值為 NULL。

@new_cmptlevel = ]  version

資料庫要與之相容的 SQL Server 的版本。version 的資料類型為 tinyint,預設值為 NULL。該值必須為下列值之一:

80 = SQL Server 2000

90 = SQL Server 2005

100 = SQL Server 2008

傳回碼值

0(成功)或 1(失敗)

 

注意事項:後續版本的 Microsoft SQL Server 將刪除該功能。請不要在新的開發工作中使用該功能,並儘快修改當前還在使用該功能的應用程式。 改為使用 ALTER DATABASE 相容層級。 關於備份,可以看我的另外一篇文章:第一篇——第一文 SQL Server 備份基礎

文章來源:如何把SQLServer資料庫從高版本降級到低版本?

如何把SQLServer資料庫從高版本降級到低版本?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.