Home Database Mysql Tutorial MS SQL 排序规则总结

MS SQL 排序规则总结

Jun 07, 2016 pm 05:38 PM
Summarize sort rule

排序规则术语 什么是排序规则呢? 排序规则根据特定语言和区域设置标准指定对字符串数据进行排序和比较的规则。SQL Server 支持在单个数据库中存储具有不同排序规则的对象。MSDN解释:在 Microsoft SQL Server 中,字符串的物理存储由排序规则控制。排序规则

排序规则术语

 

什么是排序规则呢? 排序规则根据特定语言和区域设置标准指定对字符串数据进行排序和比较的规则。SQL Server 支持在单个数据库中存储具有不同排序规则的对象。MSDN解释:在 Microsoft SQL Server  中,字符串的物理存储由排序规则控制。排序规则指定表示每个字符的位模式以及存储和比较字符所使用的规则

当 Transact-SQL 语句在具有不同排序规则设置的不同数据库上下文中运行时,其运行结果可能会不同。如果可能,请为您的组织使用标准化排序规则。这样就不必显式指定每个字符或 Unicode 表达式中的排序规则。如果必须使用具有不同排序规则和代码页设置的对象,请对查询进行编码,以考虑排序规则的优先顺序规则。

排序规则指定了表示每个字符的位模式。它还指定了用于排序和比较字符的规则。排序规则的特征是区分语言、区分大小写、区分重音、区分假名以及区分全半角。如下所示:

Chinese_PRC_CI_AS   前半部份:指UNICODE字符集,Chinese_PRC_指针对大陆简体字UNICODE的排序规则,CI表示不区分大小写,AS表示区分重音。

排序规则的后半部份即后缀 含义:

_BIN         指定使用向后兼容的二进制排序顺序。

_BIN2        指定使用 SQL Server 2005 中引入的码位比较语义的二进制排序顺序。

_Stroke      按笔划排序

_CI(CS)      是否区分大小写,CI不区分,CS区分(case-insensitive/case-sensitive)

_AI(AS)      是否区分重音,AI不区分,AS区分(accent-insensitive/accent-sensitive)

_KI(KS)      是否区分假名类型,KI不区分,KS区分(kanatype-insensitive/kanatype-sensitive)

_WI(WS)      是否区分全半角, WI不区分,WS区分(width-insensitive/width-sensitive)

区分大小写:如果想让比较将大写字母和小写字母视为不等,请选择该选项。

区分重音:如果想让比较将重音和非重音字母视为不等,请选择该选项。如果选择该选项,
比较还将重音不同的字母视为不等。
区分假名:如果想让比较将片假名和平假名日语音节视为不等,请选择该选项。
区分宽度:如果想让比较将半角字符和全角字符视为不等,请选择该选项。

查看数据库支持哪些排序规则可以通过下面系统函数查看:

select * from ::fn_helpcollations();

 

排序规则类型

      

SQL Server 提供了两组排序规则:Windows 排序规则和 SQL Server 排序规则。具体参考MSDN,这里不做过多赘述。

 

查看服务器排序规则

SELECT SERVERPROPERTY(N'Collation')

 

查看数据库排序规则

SQL 1: , ) SQL 2: , collation_name sys.databases;

 

 

查看列排序规则

SQL 1:

SELECT c.object_id, c.name, t.name, c.collation_name FROM sys.columns c LEFT JOIN sys.types t on t.system_type_id = c.system_type_id ();

 

修改服务器排序规则

 

修改服务器的排序规则的原因千差万别,大部分情况是由于安装的时候,忽略了服务器排序规则这个选项设定,没有事前规划好,等到将数据库还原或迁移到新服务器上,测试过程中才发现问题。

修改服务器排序规则,其实是重新生成 master、model、msdb 和 tempdb 系统数据库时,将删除这些系统数据库,然后在默认位置重新创建。 如果在重新生成语句中指定了新排序规则,则将使用该排序规则设置创建系统数据库。 用户对这些数据库所做的所有修改都会丢失。 例如,账号信息、作业、链接服务器等等。

MSDN关于设置和更改服务器排序规则

SQL 2005

    start /wait setup.exe /qb INSTANCENAME=MSSQLSERVER REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=test SQLCOLLATION=SQL_Latin1_General_CP1_CI_AI

SQL 2008

    Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName  /SQLSYSADMINACCOUNTS=accounts /[ SAPWD= StrongPassword ]  /SQLCOLLATION=CollationName

clip_image002

服务器排序规则修改起来看似很简单,其实不然,一不小心,就会让你万劫不复。我在实践中就碰到过一次疏忽了某个步骤,结果让我惊出一身冷汗。下面是我自己实施的步骤:

Step 1: 首先备份数据库(包括系统数据库和用户数据库)。记得千万不要漏掉了系统数据库备份。有备才能无患,否则每一步操作,你总要提心吊胆。

Step 2: 在文档上记录下你修改过的一些服务器配置值。例如,在SQL SERVER 2008中,你有可能启用backup compression default ;在某个32位数据库开启了awe enabled 选项,那么修改服务器排序规则后,你需要重新应用、配置这些值。以免遗漏,导致数据库性能等问题。

Step 3:记录一下系统数据库的数据文件和日志文件的所在路径。 重新生成系统数据库会将所有系统数据库安装到其原位置。 如果你没有移动过系统数据库数据库文件或日志文件,这部可以忽略,,像很多时候,为了I/O性能等原因,可能移动过这些系统数据库文件和日志文件。

Step 4: 用文档将登录名(logins)和相关密码整理出来。因为修改服务器排序规则,实则重建系统数据库master、msdb、tempd等,登录名等信息会全部没有,需要重新创建、配置。

Step 5: 生成已有作业的SQL脚本。方便修改服务器排序规则后,重新创建、部署作业。道理同上。

Step 6: 生成已有链接服务器的排序规则,方便修改服务器排序规则后,重新创建、部署链接服务器。道理同上。

Step 7: 整理一下数据库邮件配置文件和已经创建的账号,方便修改服务器排序规则后,重新配置。

Step 8: 如果在实例上有配置发布—订阅等,那么也需要整理这些相关的脚本、文档。

Step 9: 分离用户创建的数据库(这一步其实没有必要)。

Step 10:修改服务器排序规则

Step 11: 附加Step 9分离的数据库。

Step 12:解决孤立账号、配置作业、链接服务器…..

当然,看似简单的操作过程,其实在不同的环境下,你总会遇到一些意外情况

例1:

D:\软件工具\SQL SERVER 2008>Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=M

SSQLSERVER /SQLSYSADMINACCOUNTS=sa /SAPWD=123456 /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS

Microsoft (R) SQL Server 2008

The following error occurred:

指定的 sa 密码不满足强密码要求。有关强密码要求的更多信息,请参见安装程序帮助或 SQL Server 2008 联机丛书中的“数据库引擎配置 - 帐户设置”。

Error result: -2068578304

Result facility code: 1204

Result error code: 0

Please review the summary.txt log for further details

clip_image004

这个需要你修改sa的密码,满足强密码要求就可解决这个问题。

例2: 不小心将/SAPWD中间多了几个空格,结果报如下错误。

image

 

例3:附加数据库时,没有用sa账号,而是用sa创建的windows 身份登录验证账号附加数据库,结果报如下错误,改用sa账号附加,问题解决

image

 

另外以前也碰到过两个异常情况,一下子很难重现,以后遇到在补上。

 

clip_image008

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to sort photos by date taken in Windows 11/10 How to sort photos by date taken in Windows 11/10 Feb 19, 2024 pm 08:45 PM

This article will introduce how to sort pictures according to shooting date in Windows 11/10, and also discuss what to do if Windows does not sort pictures by date. In Windows systems, organizing photos properly is crucial to making it easy to find image files. Users can manage folders containing photos based on different sorting methods such as date, size, and name. In addition, you can set ascending or descending order as needed to organize files more flexibly. How to Sort Photos by Date Taken in Windows 11/10 To sort photos by date taken in Windows, follow these steps: Open Pictures, Desktop, or any folder where you place photos In the Ribbon menu, click

How to sort emails by sender, subject, date, category, size in Outlook How to sort emails by sender, subject, date, category, size in Outlook Feb 19, 2024 am 10:48 AM

Outlook offers many settings and features to help you manage your work more efficiently. One of them is the sorting option that allows you to categorize your emails according to your needs. In this tutorial, we will learn how to use Outlook's sorting feature to organize emails based on criteria such as sender, subject, date, category, or size. This will make it easier for you to process and find important information, making you more productive. Microsoft Outlook is a powerful application that makes it easy to centrally manage your email and calendar schedules. You can easily send, receive, and organize email, while built-in calendar functionality makes it easy to keep track of your upcoming events and appointments. How to be in Outloo

PHP development: How to implement table data sorting and paging functions PHP development: How to implement table data sorting and paging functions Sep 20, 2023 am 11:28 AM

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Summarize the usage of system() function in Linux system Summarize the usage of system() function in Linux system Feb 23, 2024 pm 06:45 PM

Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

C++ program: rearrange the position of words in alphabetical order C++ program: rearrange the position of words in alphabetical order Sep 01, 2023 pm 11:37 PM

In this problem, a string is given as input and we have to sort the words appearing in the string in lexicographic order. To do this, we assign an index starting from 1 to each word in the string (separated by spaces) and get the output in the form of sorted indices. String={"Hello","World"}"Hello"=1 "World"=2 Since the words in the input string are in lexicographic order, the output will print "12". Let's look at some input/result scenarios - Assuming all words in the input string are the same, let's look at the results - Input:{"hello","hello","hello"}Result:3 Result obtained

How does Arrays.sort() method in Java sort arrays by custom comparator? How does Arrays.sort() method in Java sort arrays by custom comparator? Nov 18, 2023 am 11:36 AM

How does Arrays.sort() method in Java sort arrays by custom comparator? In Java, the Arrays.sort() method is a very useful method for sorting arrays. By default, this method sorts in ascending order. But sometimes, we need to sort the array according to our own defined rules. At this time, you need to use a custom comparator (Comparator). A custom comparator is a class that implements the Comparator interface.

How to sort WPS scores How to sort WPS scores Mar 20, 2024 am 11:28 AM

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

See all articles