如何解决连接和联合

时间:2018-01-10 11:13:39

标签: sql sql-server collation

使用 Table1 Table2 Table3 表时,我收到以下错误

Microsoft SQL:无法解决UNION ALL运算符中“ SQL_Latin1_General_CP1_CI_AS ”与“ Latin1_General_CI_AS ”之间的排序规则冲突,以进行DISTINCT操作。

DataBase Collation  is  SQL_Latin1_General_CP1_CI_AS    

Table1  Collation  is   SQL_Latin1_General_CP1_CI_AS    
Table2  Collation  is   SQL_Latin1_General_CP1_CI_AS
Table3  Collation  is   SQL_Latin1_General_CP1_CI_AS


SELECT
      b.[BudgetID]
      ,b.[Budget]
      ,b.[BudgetType]
      ,b.[BudgetStatus]
      ,b.[BudgetStartDate]
      ,b.[BudgetEndDate]
      ,b.[ActualStartDate]
      ,b.[ActualEndDate]
      ,b.[PlannedBudget]
      ,b.[ActualSpend]
      ,b.[ProductLineName]
      ,b.[ProductLineKey]
      ,b.[ProductName]
      ,b.[ProductKey]
      ,b.[PlannedLeads]
      ,b.[ActualLeads]

  FROM [dbo].[Table1]b
  inner join  [dbo].[Table2] c on c.budgetName = b.productlinename
  inner join [dbo].[Table3] p on p.productline = b.productlinename

无法在等于操作的情况下解决“SQL_Latin1_General_CP1_CI_AS”和“Latin1_General_CI_AS”之间的排序规则冲突。

2 个答案:

答案 0 :(得分:0)

只需在两个操作中添加COLLATE DATABASE_DEFAULT即可完成。归类可能不同,不仅在数据库级别,而且在列级别上。这可能就是这里发生的事情。

答案 1 :(得分:0)

COLLATE添加到JOIN语法中,如此

  FROM [dbo].[Table1]b
  inner join  [dbo].[Table2] c on c.budgetName COLLATE SQL_Latin1_General_CP1_CI_AS = b.productlinename COLLATE SQL_Latin1_General_CP1_CI_AS 
  inner join [dbo].[Table3] p on p.productline COLLATE SQL_Latin1_General_CP1_CI_AS = b.productlinename COLLATE SQL_Latin1_General_CP1_CI_AS