一、实例介绍
SQL语句大小写到底是否区分呢?我们先从下面的这个例子来看一下:
例:
--> 创建表,插入数据:
declare @maco table (number int,myvalue varchar(2)) insert into @maco select 1,'aa' union all select 2,'Aa' union all select 3,'aA' union all select 4,'AA' --不区分大小写 select * from @maco WHERE nn='aa' --区分大小写 select * from @maco WHERE nn='aa' collate Chinese_PRC_CS_AI
注:我用的是Sql Server 2005。
从上面的例子我们可以看到第一句不区分,而第二句区分。可是每个语句不能都加上个collate Chinese_PRC_CS_AI吧?
我们可以这样,看下面的例子:
例:
--不区分大小写 ALTER TABLE My_table ALTER Column colname nvarchar(100) collate Chinese_PRC_CI_AS --区分大小写 ALTER TABLE My_table ALTER Column colname nvarchar(100) collate Chinese_PRC_CS_AS
从上面的例子我们可以看到第一句不区分,而第二句区分。可是我们不能每个表都进行一下修改吧?
我们可以再这样,看下面的例子:
例:
--不区分大小写 ALTER DATABASE databasename collate Chinese_PRC_CI_AS --区分大小写 ALTER DATABASE databasename collate Chinese_PRC_CS_AS
批量修改数据库中所有表内字段大小写敏感
如:
DECLARE @collate nvarchar(100); DECLARE @table nvarchar(255); DECLARE @column_name nvarchar(255); DECLARE @column_id int; DECLARE @data_type nvarchar(255); DECLARE @max_length int; DECLARE @row_id int; DECLARE @sql nvarchar(max); DECLARE @sql_column nvarchar(max); DECLARE @is_nullable int; DECLARE @nullornot nvarchar(255); SET @collate = 'Chinese_PRC_CS_AS'; --不区分大小写: Chinese_PRC_CI_AS 区分大小写 :Chinese_PRC_CS_AS DECLARE local_table_cursor CURSOR FOR SELECT [name] FROM sysobjects WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1 OPEN local_table_cursor FETCH NEXT FROM local_table_cursor INTO @table WHILE @@FETCH_STATUS = 0 BEGIN DECLARE local_change_cursor CURSOR FOR SELECT ROW_NUMBER() OVER (ORDER BY c.column_id) AS row_id , c.name column_name , t.Name data_type , c.max_length , c.column_id , c.is_nullable FROM sys.columns c JOIN sys.types t ON c.system_type_id = t.system_type_id LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id WHERE c.object_id = OBJECT_ID(@table) ORDER BY c.column_id OPEN local_change_cursor FETCH NEXT FROM local_change_cursor INTO @row_id, @column_name, @data_type, @max_length, @column_id,@is_nullable WHILE @@FETCH_STATUS = 0 BEGIN IF (@max_length = -1) OR (@max_length > 4000) SET @max_length = 4000; IF (@is_nullable = 1) SET @nullornot=' not null' ELSE SET @nullornot='' IF (@data_type LIKE '%char%') BEGIN TRY SET @sql = 'ALTER TABLE ' + @table + ' ALTER COLUMN ' + @column_name + ' ' + @data_type + '(' + CAST(@max_length AS nvarchar(100)) + ') COLLATE ' + @collate +' ' +@nullornot PRINT @sql EXEC sp_executesql @sql END TRY BEGIN CATCH PRINT 'ERROR: Some index or constraint rely on the column ' + @column_name + '. No conversion possible.' PRINT @sql END CATCH FETCH NEXT FROM local_change_cursor INTO @row_id, @column_name, @data_type, @max_length, @column_id,@is_nullable END CLOSE local_change_cursor DEALLOCATE local_change_cursor FETCH NEXT FROM local_table_cursor INTO @table END CLOSE local_table_cursor DEALLOCATE local_table_cursor GO
二、参数介绍
Chinese_PRC_指针对大陆简体字UNICODE的排序规则。
排序规则的后半部份(即后缀)含义:
_BIN 二进制排序_CI(CS) 是否区分大小写:CI不区分,CS区分
_AI(AS) 是否区分重音:AI不区分,AS区分
_KI(KS) 是否区分假名类型:KI不区分,KS区分
_WI(WS) 是否区分宽度:WI不区分,WS区分
三、整体介绍
在安装SQL时,我们可以选择区分大小写或安装完以后重建mastar,再选择区分大小
下面是rebuildm.exe 的路径:
C:/Program Files/Microsoft SQL Server/80/Tools/Binn/rebuildm.exe
若要修改排序规则,按照上面的参数设置即可。
若只修改一个表,用ALTER TABLE语句
若修改一个库的默认排序规则,用ALTER DATABASE语句
若修改整个服务器的默认排序规则,用Rebuildm.exe重建master库
时间: 2024-11-06 03:44:44