在部署ERP应用时,经常会碰到在开发环境下,没有问题,部署到服务器上,就出错。不同语言版本的操作系统、不同的数据库版本,等等因素的数据库排序规则的问题。比如在安装SQL Server 时取默认的规则,中文环境下,会设置为Chinese_PRC_CI_AS,但我们需要的规则应该是SQL_Latin1_General_CP1_CI_AS。在数据操作过程中,这个不一致会导致SQL语句的字符串相等报错。错误语句如下
The default collation of database 'pco' cannot be set to SQL_Latin1_General_CP1_CI_AS.
对于报表的存储过程,错误更严重。每个有建立临时表,比较字符串的地方,都会报错。
当安装时设定的SQL Server排序规则为Chinese_PRC_CI_AS,建数据库时又选定为默认规则,则数据库的排序规则也是Chinese_PRC_CI_AS,运行如下的脚本把它改变为SQL_Latin1_General_CP1_CI_AS:
Alter database [Database name] collate SQL_Latin1_General_CP1_CI_AS
当数据库中的Index,Constraint依赖于数据库排序规则时,上面的语句很难成功运行。
CodeProject上面有一篇文章Easy way to change collation of all database objects in SQL Server
这里的程序以依据这篇文章介绍的原理,开发出来对它封装一下,简化操作。
如图所示的软件界面,设定SQL Server实例,选取数据库名字,输入要改变成的排序规则,点击处理,即可生成包含7个步骤运行的SQL语句,把这7个SQL语句依次放到数据库中执行一次,即可改变它的排序规则。
有五个关键的存储过程要先运行CreateKeyIndex.sql ,DropKeyIndex.sql,ScriptCreateTableKeys.sql,ScriptDropTableKeys.sql,G
enerateDefaultCollation.sql 。目的如它的名字所示,创建索引和约束。
在编程过程,原本是想把这几个SQL语句直接以SqlCommand的方式发送到SQL Server中去运行,但是它会报一个SQL语句太长的错误,求助于Google没有解决,只好在SQL Server服务器中手动运行这几个脚本。
后面生成的SQL语句,也都应用此政策(policy),一方面是没有解决SQL语句太长的问题,另一方面,从效率上来说,直接在SQL Server Management Studio中运行SQL语句的效率是最好的。
DROP INDEX [PRCALW_INDEX2] ON [dbo].[WorkCentreCalendar]; DROP INDEX [PRCALW_INDEX0] ON [dbo].[WorkCentreCalendar]; ALTER TABLE [dbo].[WorkCentreCalendar] DROP CONSTRAINT [PRCALW_PK];
ALTER TABLE [CalendarTemplate] ALTER COLUMN [REF_NO] nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [FIRST_DAY] char(1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [FIRST_SHIFT_BEGIN] nchar(4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [SENCOND_SHIFT_BEGIN] nchar(4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [THIRD_SHIFT_BEGIN] nchar(4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [MON_HOLIDAY] char(1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [WEN_HOLIDAY] char(1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ALTER TABLE [CalendarTemplate] ALTER COLUMN [WEN_HALFDAY] char(1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE [dbo].[Account] ADD CONSTRAINT [GBACCT_PK] PRIMARY KEY CLUSTERED ( [ACCT_NO] ) WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]; CREATE UNIQUE NONCLUSTERED INDEX [GBACCT_INDEX0] ON [dbo].[Account] ( [RECNUM] ) WITH ( PAD_INDEX = OFF, FILLFACTOR = 70, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]; CREATE UNIQUE NONCLUSTERED INDEX [GBACCT_INDEX2] ON [dbo].[Account] ( [ACCT_NAME], [ACCT_NO] ) WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY];
Declare @action nvarchar(20) SET @action='Disable' --Enable Disable --Declare @action nvarchar(20) --SET @action='Enable' Declare @sql nvarchar(200) Declare @Name nvarchar(200) Declare @TableName nvarchar(200) Declare Cur Cursor For SELECT name,OBJECT_NAME(parent_object_id) FROM sys.check_constraints Open Cur Fetch next From Cur Into @Name,@TableName While @@fetch_status=0 BEGIN IF @action='Disable' SELECT @sql='alter table ' + @TableName +' NOCHECK CONSTRAINT ' + @Name ELSE SELECT @sql='alter table ' + @TableName +' WITH CHECK CHECK CONSTRAINT ' + @Name Fetch Next From Cur Into @Name,@TableName End Close Cur Deallocate Cur
ALTER DATABASE MIS SET SINGLE_USER WITH ROLLBACK IMMEDIATE ALTER DATABASE MIS COLLATE SQL_Latin1_General_CP1_CI_AS ALTER DATABASE MIS SET ARITHABORT ON ALTER DATABASE MIS SET MULTI_USER
Declare @action nvarchar(20) SET @action='Enable' --Enable Disable --Declare @action nvarchar(20) --SET @action='Enable' Declare @sql nvarchar(200) Declare @Name nvarchar(200) Declare @TableName nvarchar(200) Declare Cur Cursor For SELECT name,OBJECT_NAME(parent_object_id) FROM sys.check_constraints Open Cur Fetch next From Cur Into @Name,@TableName While @@fetch_status=0 BEGIN IF @action='Disable' SELECT @sql='alter table ' + @TableName +' NOCHECK CONSTRAINT ' + @Name ELSE SELECT @sql='alter table ' + @TableName +' WITH CHECK CHECK CONSTRAINT ' + @Name Fetch Next From Cur Into @Name,@TableName End Close Cur Deallocate Cur
第5步和第7步使用的SQL语句基本相同,遍历所有的约束,生成NOCHECK和WITH CHECK的SQL语句。
您可以到CodeProject网站中下载源代码,文章标题是
Easy way to change collation of all database objects in SQL Server。
刚开始读这篇文章,有些吃力,消化不了。在Google寻找解决办法过程中,最后还是觉得他提出的方案是很好的。在这里,我向前走一步,做一个固化的程序来解决问题,让步骤明了简明,著文以分享给大家,欢迎指正。