gpt4 book ai didi

sql-server - varchar 值隐式转换为 varchar - 排序规则冲突

转载 作者:行者123 更新时间:2023-12-03 03:19:15 28 4
gpt4 key购买 nike

运行此脚本时出现以下错误。我尝试过使用以下内容:整理 Latin1_General_CI_AS。请问可以排序吗?谢谢

Msg 457, Level 16, State 1, Line 8
Implicit conversion of varchar value to varchar cannot be performed because the collation of the value is unresolved due to a collation conflict

DECLARE @AccountID INT 
SET @AccountID = 12

SELECT TOP 1 ac.AccountID,
co.Email,
ao.AccountOptionID
FROM CRM.acc.Account ac
INNER JOIN CRM.[profile].[Profile] pr
ON pr.ProfileID = ac.ProfileFK
INNER JOIN CRM.[profile].Contact co
ON pr.ProfileID = co.ProfileFK
LEFT JOIN CRM.acc.[AccountOption] ao
ON ao.AccountFK = ac.AccountID
LEFT JOIN (
SELECT OptionID
FROM CRM.acc.[Option]
WHERE [Name] = 'SMS messages') op
ON op.OptionID = ao.OptionFK
WHERE ac.AccountID = @AccountID

UNION ALL

SELECT u.UnsubscribeID,
u.EmailAddress,
u.SentEmailFK
FROM Email.dbo.Unsubscribe u
INNER JOIN (
SELECT CASE
WHEN AccountTypeFK = 2 THEN OnlineBillingEmail
ELSE EmailBillingEmail
END [EmailAddress]
FROM CRM.acc.Account
WHERE AccountID = @AccountID
) ace
ON ace.EmailAddress COLLATE DATABASE_DEFAULT = u.EmailAddress COLLATE DATABASE_DEFAULT
WHERE ISNULL(ace.EmailAddress, '') != ''

最佳答案

您的数据库似乎有不同的排序规则类型。然后,在连接表或连接到其他数据库中的表时可能会遇到问题,如本例所示。为了解决这个问题,您可以指定列的排序规则,或者在连接两列时使用 COLLATE 子句强制进行排序规则。检查more info about collation in MSDN

您还需要指定使用有问题列的 COLLATE 子句。 Check this answer ,它回答了一个非常相似的问题

最好在全局范围内坚持使用单一排序规则。否则你会遇到问题。

查看different collation styles的详细解释

已编辑:要检查列排序规则,请使用此代码段

SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN (SELECT OBJECT_ID
FROM sys.objects
WHERE type = 'U'
AND name = 'your_table_name'
)
AND name = 'your_column_name'

编辑:添加片段以获取数据库上具有不同排序规则的所有列

SELECT [TABLE_NAME] = OBJECT_NAME([id]),
[COLUMN_NAME] = [name],
[COLLATION_NAME] = collation
FROM syscolumns
WHERE collation <> 'your_database_collation_type'
AND collation IS NOT NULL
AND OBJECTPROPERTY([id], N'IsUserTable')=1

关于sql-server - varchar 值隐式转换为 varchar - 排序规则冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com