修改sql server实例、数据库、表、字段的排序规则

转自:http://blog.51cto.com/jimshu/1095780

 概念与详情请参考:字符编码与排序规则:https://www.cnblogs.com/gered/p/9145123.html

零、修改表达式排序规则

问题1:

  

 

解决:

set @mdf=(SELECT dsize/1024 FROM #tf_driver a,db_tank..sysfiles b WHERE b.name='Db_Tank_Data' and a.driver COLLATE chinese_prc_ci_as =LEFT(b.FILENAME,1))

set @ldf=(SELECT dsize/1024 FROM #tf_driver a,db_tank..sysfiles b WHERE b.name='Db_Tank_Log' and a.driver COLLATE chinese_prc_ci_as =LEFT(b.FILENAME,1))

 

问题2:

   

解决:见最后一行

select t1.name,t1.system_type_id,t1.user_type_id,t1.max_length,t2.* from (
select * from test2.sys.columns where object_id=object_id('Sys_Users_History')
) t1 join (
select t2.name tab_name,t1.name,t1.system_type_id,t1.user_type_id,t1.max_length
from [10.1.4.234].db_tank.sys.columns t1 
join [10.1.4.234].db_tank.dbo.sysobjects t2  on t1.object_id=t2.id and t2.name = 'Sys_Users_History'
) t2 on t1.name=  t2.name collate Chinese_PRC_90_CI_AI

 

 

一、修改SQL Server服务器(实例)的排序规则

  以下实验使用了SQL Server 2008 R2的默认实例,将Chinese_PRC_CI_AS修改成SQL_Latin1_General_CP1_CI_AS。

1. 停止SQL Server实例服务

2. 打开“命令提示符”,转到SQL Server的安装目录

C:\Users\Administrator> cd "\Program Files\Microsoft SQL Server\100\Setup Bootstrap\SQLServer2008R2"

3. 运行setup 

C:\Program FIles\Microsoft SQL Server\100\Setup Bootstrap\SQLServer2008R2> 
setup /QUIET /ACTION=REBUILDDATABASE /instancename=mssqlserver /SQLSYSADMINACCOUNTS=administrator /sqlcollation=SQL_Latin1_General_CP1_CI_AS

4. 启动SQL Server实例服务 

5. 验证实例的排序规则

  

6. 验证系统数据库(master、model、msdb、tempdb)的排序规则

  

 

 

说明:此操作仅影响系统数据库。对于已经存在的用户数据库无效。

 

二、修改数据库的排序规则

  以下实验将修改数据库db01的排序规则,将SQL_Latin1_General_CP1_CI_AS修改成Chinese_PRC_CI_AS。

1. SSMS图形界面

   

2. 脚本

ALTER DATABASE [db01] COLLATE Chinese_PRC_CI_AS


3. 局限性
 

(1)对于已经存在的数据,此操作并不会导致立即重新排序。

(2)如果已经有对象依赖于数据库排序规则,则更改不成功。

  

 

 

三、修改表的排序规则(依赖库规则,不能改)

  表的排序规则依赖于数据库的排序规则。不能修改。

    

 

 

四、修改列的排序规则

  建议:将这个表的数据保存到另一个临时表,重建这个表,然后从临时表将数据导入到新表。

  可以通过SSMS修改某一列的排序规则,但是,这项操作实际上还是删除这个表以及相关的约束、触发器,然后重建这个表、约束、索引、触发器。

   

  

T-SQL代码实现:

ALTER TABLE dbo.EmpBasic DROP CONSTRAINT DF__EmpBasic__FirstN__2319CD4B
ALTER TABLE dbo.EmpBasic DROP CONSTRAINT ......

CREATE TABLE dbo.Tmp_EmpBasic ( ......
FirstName nvarchar(30) COLLATE Chinese_PRC_CI_AS NULL,
...... ) ON [PRIMARY]

ALTER TABLE dbo.Tmp_EmpBasic SET (LOCK_ESCALATION = TABLE)
GRANT DELETE ON dbo.Tmp_EmpBasic TO public AS dbo
GRANT INSERT ON dbo.Tmp_EmpBasic TO public AS dbo
GRANT SELECT ON dbo.Tmp_EmpBasic TO public AS dbo
GRANT UPDATE ON dbo.Tmp_EmpBasic TO public AS dbo

ALTER TABLE dbo.Tmp_EmpBasic ADD CONSTRAINT DF__EmpBasic__FirstN__2319CD4B DEFAULT ('') FOR FirstName
ALTER TABLE dbo.Tmp_EmpBasic ADD CONSTRAINT DF__EmpBasic__Middle__240DF184 ......

SET IDENTITY_INSERT dbo.Tmp_EmpBasic ON

IF EXISTS(SELECT * FROM dbo.EmpBasic)
EXEC('INSERT INTO dbo.Tmp_EmpBasic (Company, EmpID, FirstName,......)
SELECT Company, EmpID, FirstName, ...... FROM dbo.EmpBasic WITH (HOLDLOCK

TABLOCKX)')


SET IDENTITY_INSERT dbo.Tmp_EmpBasic OFF

DROP TABLE dbo.EmpBasic
EXECUTE sp_rename N'dbo.Tmp_EmpBasic', N'EmpBasic', 'OBJECT'

CREATE UNIQUE CLUSTERED INDEX EmpID ON dbo.EmpBasic
(Company, EmpID ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,

ALLOW_PAGE_LOCKS = OFF) ON [PRIMARY]

CREATE NONCLUSTERED INDEX FirstName ON dbo.EmpBasic
( Company, FirstName, LastName ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,

ALLOW_PAGE_LOCKS = OFF) ON [PRIMARY]

create trigger _ti_EmpBasic ON dbo.EmpBasic for insert as
begin
......
end
posted @ 2018-07-27 12:18  郭大侠1  阅读(2626)  评论(0编辑  收藏  举报