当前位置:网站首页>(四)Sql Server中的字符集(排序规则)
(四)Sql Server中的字符集(排序规则)
2022-04-22 06:13:00 【詹姆斯搞死你】
字符集作用:
SQL Server作为一个国际化产品,支持多语言环境。在SQL Server中,字符集被称为排序规则(即Collation)。排序规则不仅影响记录行的sort顺序,还影响中文显示是否乱码等
排序规则可在3处地方设置:
服务器级别 =>instances
db库级别 =>databases
表列级别 =>columns
SQL Server默认情况下对Collation继承的方式,也就是说,如果instances使用的排序规则是"Chinese_PRC_CI_AS",那么databases如果不设置字符集的话就会向上继承,就也是"Chinese_PRC_CI_AS",
--查询当前SQL Server服务器的排序规则
SELECT SERVERPROPERTY(N'Collation')
--------------------------------------------------
Chinese_PRC_CI_AS
关于如何修改排序规则,本文不再赘述,有需要的可自行搜索,一般采用默认的排序集就行了
这里需注意:SQL Server的排序规则只影响字符型的列,如char, varchar, text, nchar, nvarchar, ntext,因此目录视图sys.columns中非字符型的排序规则显示为NULL
那么当SQL Server中instances与databases对排序规则设置不一致时,将直接导致临时表#或##不能正常使用(临时表的列默认继承tempdb的排序规则,而tempdb则继承了instances的排序规则)。
如:
--在mydb中创建一张表collation_test
CREATE TABLE collation_test (hyper varchar(10));
GO
--创建临时表collation_temp
CREATE TABLE #collation_temp (hyper varchar(10));
GO
连接查询上述临时表的内容时,将出现如下报错信息:
--查询报错
SELECT *
FROM collation_test l
LEFT JOIN #collation_temp c
ON l.hyper = c.hyper;
--------------------------------------------
Msg 468, Level 16, State 9, Line 4
无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "SQL_Latin1_General_CP1_CI_AS" 之间的排序规则冲突。
所以我们需要保证在进行查询的时候需要保证,所查表字符集排序是一致的
同时instances的排序规则设置会影响SQL Server数据的导入导出功能。
通常我们遇到的另一个问题是:通过SSMS(即SQL Server Management Studio)插入(insert)的中文,在查询时显示乱码(即问号?)。
--在上述表collation_test插入中文
INSERT INTO collation_test VALUES ('东')
--查询表collation_test的记录
select * from collation_test
显示乱码:

综上述,我们应尽可能的正确设置SQL Server排序规则:
- 正确的设置SQL Server排序规则 ,保持instances、databases、columns中3处排序规则一致,推荐使用Chinese_PRC_CI_AS(默认值)
- 尽可能使用nvarchar等Unicode类型,而非varchar类型
参考资料
Setting and Changing the Database Collation
https://msdn.microsoft.com/en-us/library/ms175835(v=sql.105).aspx
sys.columns (Transact-SQL)
https://msdn.microsoft.com/en-us/library/ms176106(v=sql.120).aspx
Collation and International Terminology
https://msdn.microsoft.com/en-us/library/ms143726(v=sql.105).aspx
版权声明
本文为[詹姆斯搞死你]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42154902/article/details/117199897
边栏推荐
- SQL server stored procedure development notes - piecemeal problems and operations on operation files
- .NET学习笔记(二)----无处不在的反射(包含读取json的方法)
- Comparison and improvement of Nacos service registration center
- 关于Log4Net和NLog使用的帮助文档
- Practice of using generics and reflection (including a generic cache) -- handwritten ORM ORM framework
- C语言 | 指针
- 【SVN】Subversion安装使用笔记
- 搭建ES6开发环境,实时编译
- Pixhawk4+Up Board / NUC Implement VIO By Deploying T265
- C daily development notes -- solve the problem that one set references another set, resulting in changes together
猜你喜欢

Difference between analog IC design and digital IC design, including salary table

写一个方法sanjiao(a, b, c),判断三个参数是否能构成一个三角形,如果不能则抛出异常IllegalArgumentException,显示异常信息a,b,c”不能构成三角形”,如果可以

Format control of format() method

Nacos服务消费者注册和负载均衡

Build ES6 development environment and compile in real time

定义一个抽象的Role类有姓名年龄性别爱好等成员变量要求尽可能隐藏所有变量(能够私有就私有)再通过Get()和Set()方法对各变量进行读写,其中龄必须在0到150岁性别必须是男或者女姓名必须是2个字

Introduction to IC Analog Layout - learning notes on layout Basics (3)

14行代码完成任意选择图片爬取

How to become an IC Verification Engineer?

安裝和修改uTools及vscode插件安裝路徑
随机推荐
Matlab: remove audio signal noise
Shift left and right
C#日常开发随手记------获取华为云桶中ZIP内的所有文件(包含System.NotSupportedException:“此流不支持查找操作“解决方法)
XPath of crawler notes
ASP. Net daily development notes ---- parsing and transforming XML
[SVN] subversion installation notes
ASP. Net daily development notes ----- JS script executed in the background
把上一次作业第一部分,有参数的类,改成无参数方式呈现,功能不变。
. net daily thoughts - compare two JSON files and get data with the same tag but different values
Robomaster Dajiang flying hand assessment
Nacos服务提供者注册
Introduction to IC Analog Layout - learning notes on layout Basics (5)
[bug notes] the sessionstorage data cannot be obtained after the page is refreshed
Nacos service consumer registration and load balancing
macOS安装redis并设置服务自启动
安裝和修改uTools及vscode插件安裝路徑
Nacos服务注册中心对比提升
提示用户输入其名字 用户作出响应后 将其名字写 入到文件guest.txt 中 程序判断当不等于n的时候,就执行 创建文件data.txt,文件共10万行,每行存放一个1~100之间的随机一个整数
Preparation before analog circuit board commissioning_ Analog circuit board
Quotient principle of modular division