如何在 SQL Server 中设置连接的排序规则?

发布于 2024-12-01 18:52:26 字数 2144 浏览 4 评论 0原文

如何设置 SQL Server 在该连接期间将使用的排序规则?

直到我连接到 SQL Server 后我才知道我想使用什么排序规则。

例如,语言为 fr-IT 的浏览器已连接到该网站。我在该连接上运行的任何查询都希望遵循法语、意大利变体排序规则。

我设想了一个假设的连接级别属性,类似于SET ANSI_NULLS OFF,但对于排序规则1

SET COLLATION_ORDER 'French_CI_AS'

SELECT TOP 100 FROM Orders
ORDER BY ProjectName

以及越来越

SELECT * FROM Orders
WHERE CustomerID = 3277 
AND ProjectName LIKE '%l''ecole%'

UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName = 'Cour de l''école'

同时,当中文客户连接:

SET COLLATION_ORDER Chinese_PRC_CI_AI_KS_WS

SELECT TOP 100 FROM Orders
ORDER BY ProjectName

SELECT * FROM Orders
WHERE CustomerID = 3277 
AND ProjectName LIKE '學校'

UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName = '學校的操場'

现在我可以更改系统中的每个SELECT语句以允许我传递排序规则:

SELECT TOP 100 FROM Orders
WHERE CustomerID = 3278
ORDER BY ProjectName COLLATE French_CI_AS

但是您不能将排序规则顺序作为参数传递到存储过程:

CREATE PROCEDURE dbo.GetCommonOrders 
   @CustomerID int, @CollationOrder varchar(50)
AS

SELECT TOP 100 FROM Orders
WHERE CustomerID = @CustomerID
ORDER BY ProjectName COLLATE @CollationOrder

并且执行 UPDATESELECT 时,COLLATE 子句无法帮助我。

注意:数据库中的所有字符串列都已经是 ncharnvarcharntext。我不是在谈论应用于服务器、数据库、表或非 unicode 列的默认排序规则(即 charvarchartext)。我说的是 SQL Server 在比较和排序字符串时使用的排序规则。


如何指定每个连接的排序规则?

另请参阅

1 出现区域设置问题的假设 SQL

How can i set the collation SQL Server will use for the duration of that connection?

Not until i connect to SQL Server do i know what collation i want to use.

e.g. a browser with language fr-IT has connected to the web-site. Any queries i run on that connection i want to follow the French language, Italy variant collation.

i envision a hypothetical connection level property, simlar to SET ANSI_NULLS OFF, but for collation1:

SET COLLATION_ORDER 'French_CI_AS'

SELECT TOP 100 FROM Orders
ORDER BY ProjectName

and later

SELECT * FROM Orders
WHERE CustomerID = 3277 
AND ProjectName LIKE '%l''ecole%'

and later

UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName = 'Cour de l''école'

At the same time, when a chinese customer connects:

SET COLLATION_ORDER Chinese_PRC_CI_AI_KS_WS

SELECT TOP 100 FROM Orders
ORDER BY ProjectName

or

SELECT * FROM Orders
WHERE CustomerID = 3277 
AND ProjectName LIKE '學校'

or

UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName = '學校的操場'

Now i could alter every SELECT statement in the system to allow me to pass in a collation:

SELECT TOP 100 FROM Orders
WHERE CustomerID = 3278
ORDER BY ProjectName COLLATE French_CI_AS

But you cannot pass a collation order as a parameter to a stored procedure:

CREATE PROCEDURE dbo.GetCommonOrders 
   @CustomerID int, @CollationOrder varchar(50)
AS

SELECT TOP 100 FROM Orders
WHERE CustomerID = @CustomerID
ORDER BY ProjectName COLLATE @CollationOrder

And the COLLATE clause can't help me when performing an UPDATE or a SELECT.

Note: All string columns in the database all are already nchar, nvarchar or ntext. i am not talking about the default collation applied to a server, database, table, or column for non-unicode columns (i.e. char, varchar, text). i am talking about the collation used by SQL Server when comparing and sorting strings.


How can i specify per-connection collation?

See also

1 hypothetical sql that exhibits locale issues

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葮薆情 2024-12-08 18:52:26

正如 marc_s 评论的,排序规则是数据库或列的属性,而不是连接的属性。

但是,您可以使用 COLLATE 关键字覆盖语句级别的排序规则。

使用你的例子:

SELECT * FROM Orders
WHERE CustomerID = 3277 
AND ProjectName COLLATE Chinese_PRC_CI_AI_KS_WS LIKE N'學校'

UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName COLLATE Chinese_PRC_CI_AI_KS_WS = N'學校的操場'

仍然,我找不到关于使用 COLLATE 和动态排序规则名称的语句,留下唯一可能的解决方案动态 SQL 和 EXEC。请参阅此social.MSDN条目 为例。

As marc_s commented, the collation is a property of a database or a column, and not of a connection.

However, you can override the collation on statement level using the COLLATE keyword.

Using your examples:

SELECT * FROM Orders
WHERE CustomerID = 3277 
AND ProjectName COLLATE Chinese_PRC_CI_AI_KS_WS LIKE N'學校'

UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName COLLATE Chinese_PRC_CI_AI_KS_WS = N'學校的操場'

Still, I cannot find a statement on using COLLATE with a dynamic collation name, leaving as only possible solution dynamic SQL and EXEC. See this social.MSDN entry for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文