varchar值隐式转换为varchar - collat​​ion冲突

时间:2012-08-23 08:33:22

标签: sql sql-server-2008 collation

运行此脚本时出现以下错误。我尝试过使用以下内容: 收集Latin1_General_CI_AS。请问它可以分类吗?感谢

消息457,级别16,状态1,第8行     无法执行varchar值到varchar的隐式转换,因为由于排序规则冲突导致值的排序规则未解决

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, '') != ''

2 个答案:

答案 0 :(得分:3)

似乎您的数据库中有不同的排序规则类型。然后,在连接表或其他数据库中的表时可能会出现问题,如本例所示。要解决此问题,您可以在连接两列时使用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

答案 1 :(得分:1)

试试这个

ace.EmailAddress COLLATE Latin1_General_CI_AS = u.EmailAddress COLLATE Latin1_General_CI_AS