如何把SQLServer数据库从高版本降级到低版本?
由于目前还广泛使用着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中运行脚本。
步骤5:通过【任务】→【导入数据】,把数据从2008导入到使用脚本创建的库上如下图,就完成了:
方法二:使用系统自带的存储过程实现:sp_dbcmptlevel ——将某些数据库行为设置为与指定的 SQL Server 版本兼容
下面是其内部实现代码:
SET QUOTED_IDENTIFIER ONSET ANSI_NULLS ONGOcreate procedure sys.sp_dbcmptlevel -- 1997/04/15@dbname sysname = NULL, -- database name to change@new_cmptlevel tinyint = NULL OUTPUT -- the new compatibility level to change toasset nocount ondeclare @exec_stmt nvarchar(max)declare @returncode intdeclare @comptlevel float(8)declare @dbid int -- dbid of the databasedeclare @dbsid varbinary(85) -- id of the owner of the databasedeclare @orig_cmptlevel tinyint -- original compatibility leveldeclare @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.0select @cmptlvl80 = 80,@cmptlvl90 = 90,@cmptlvl100 = 100-- SP MUST BE CALLED AT ADHOC LEVEL --if (@@nestlevel > 1)beginraiserror(15432,-1,-1,'sys.sp_dbcmptlevel')return (1)end-- If no @dbname given, just list the valid compatibility level values.if @dbname is nullbeginraiserror (15048, -1, -1, @cmptlvl80, @cmptlvl90, @cmptlvl100)return (0)end-- Verify the database name and get infoselect @dbid = dbid, @dbsid = sid ,@orig_cmptlevel = cmptlevelfrom master.dbo.sysdatabaseswhere name = @dbname-- If @dbname not found, say so and list the databases.if @dbid is nullbeginraiserror(15010,-1,-1,@dbname)print ' 'select name as 'Available databases:'from master.dbo.sysdatabasesreturn (1)end-- Now save the input compatibility level and initialize the return clevel-- to be the current clevelselect @input_cmptlevel = @new_cmptlevelselect @new_cmptlevel = @orig_cmptlevel-- If no clevel was supplied, display and output current level.if @input_cmptlevel is nullbeginraiserror(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)beginraiserror(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 IS CURRENT DBand (@dbid <> db_id() or is_member('db_owner') <> 1)beginraiserror(15418,-1,-1)return (1)end-- If we're in a transaction, disallow this since it might make recovery impossible.set implicit_transactions offif @@trancount > 0beginraiserror(15002,-1,-1,'sys.sp_dbcmptlevel')return (1)endset @exec_stmt = 'ALTER DATABASE ' + quotename(@dbname, '[') + ' SET COMPATIBILITY_LEVEL = ' + cast(@input_cmptlevel as nvarchar(128))-- Note: database @dbname may not exist anymoreexec(@exec_stmt)select @new_cmptlevel = @input_cmptlevelreturn (0) -- sp_dbcmptlevelGO
语法
sp_dbcmptlevel [ [ @dbname = ] name ] [ , [ @new_cmptlevel = ] version ]
参数
- [ @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(失败)
注意事项:
版权声明:本文为博主原创文章,未经博主允许不得转载。
如何把SQLServer数据库从高版本降级到低版本?相关推荐
- windows如何把已安装的nodejs高版本降级为低版本node多环境
目录 卸载本地的node 安装nvm管理工具 解压安装 安装node版本 多环境管理 linux下版本管理 n 的使用 参考资料 卸载本地的node where node 找到路径 ,然后删除 控制 ...
- windows中如何将已安装的nodejs高版本降级为低版本
第一步:先清空本地安装的node.js版本 1.按健win+R弹出窗口,键盘输入cmd,然后敲回车(或者鼠标直接点击电脑桌面最左下角的win窗口图标弹出,输入cmd再点击回车键) 然后进入命令控制行窗 ...
- windows nodejs高版本降级为低版本
按健win+R弹出窗口,键盘输入cmd,然后敲回车.然后进入命令控制行窗口,并输入where node查看之前本地安装的node的路径. 找到上面找到的路径,将node.exe所在的父目录里面的所有东 ...
- webpack 4.14.0 版本太高,无法执行相关指令,(将webpack高版本切换到低版本)--直接覆盖
(1)问题: webpack 4.14.0 版本太高,无法执行相关指令, (2) 解决办法:将高版本切换到低版本 (3)实现 webpack 4.14.0 版本太高,无法执行相关指令,指令不熟悉,高版 ...
- 红米Android降级,红米Note 5(安卓9.0 不要降级刷低版本)纯净ROOT线刷包分享,一键救砖教程,轻松刷回官方系统,流畅如初!...
红米Note 5(安卓9.0 不要降级刷低版本)纯净ROOT线刷包分享,一键救砖教程,轻松刷回官方系统,流畅如初! 发布日期:2019-05-06 来源:互联网 编辑:lanxi 红米Note ...
- cad转换器高版本转低版本_excel 高版本保存为低版本方法教程
excel有很多版本,每个人使用的版本都不同,如果你用的是高版本的,南无低版本的excel是打不开的,当然在Excel2007或是2010在保存时可以选择另存为,在另存为中选择低版本,但是这样一来就比 ...
- VMware高版本转VMware低版本
将VMware 高版本 转VMware 低版本 今天将VMware 15 版本 卸载了感觉不是很好用,就打算换成VMware 12 版本,然后就出现了一个我换之前没想到的问题就是高版本向低版本的兼 ...
- Solidworks高版本如何转换为低版本
前言 Solidworks是一款广受欢迎的三维CAD软件.由于技术的不断进步和软件的更新迭代,使用高版本的Solidworks软件制作的三维模型文件无法在低版本的软件中打开.这给工程师和设计师带来了不 ...
- 高版本转低版本_Tekla高版本模型转低版本模型插件
网上搜到的一个高版本转低版本模型的插件,只能转模型,图纸不能转.export import是一个简单的程序,能够将Tekla结构模型导出/导入到XML文件中.主要目的是将Tekla模型从新版本转换为旧 ...
最新文章
- PCIe配置空间和PCI设备中的寄存器
- 疫情下的硅谷区块链创业者
- Service Mesh 在中国工商银行的探索与实践
- php 获取京东交易账号,PHP爬虫爬取京东列表
- 重构碎片化知识_day22重构力-思维导图:让碎片化知识的零存整取
- php implode key,PHP Implode关联数组
- 数据可视化系列(四):文字图例尽眉目
- 【算法】剑指 Offer 48. 最长不含重复字符的子字符串
- TensorFlow版本的HelloWord
- c语言谢延红主编答案,C语言程序设计课程改革与实践.doc
- 数据挖掘与数据分析好书推荐
- Java不适合于作为主要编程教学语言 -- 孟岩
- vscode设置字体颜色
- java 错误代码2503_错误代码:11300001 数据集配置错误
- 弘辽科技:直通车引流逻辑。
- Origin画并列柱状图
- 线粒体靶向的纳米递送PCN-224 纳米粒子-瑞禧
- 当深度学习遇见自动文本摘要
- XP系统计算机桌面图标不见,XP电脑开机桌面上没有图标怎么办?
- 迷失在Blog(ZZ)