SQL Collation conflict when comparing to a column in a temp table

那年仲夏 提交于 2019-12-18 13:08:34

问题


I have a SQL query that compares a value in the database to a constant:

SELECT * FROM my_table
INNER JOIN #TempTable tem
    ON my_table.id = temp.id
    AND my_table.key = 'SOME STRING'

And I get the error:

Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation.

How can I get around this? (without making changes to the database)

UPDATE: I get this error even if I remove the last like (the string comparison)...


回答1:


Seems your id's are VARCHARs with different collations.

Try this:

SELECT  *
FROM    my_table
INNER JOIN
        #TempTable tem
ON      my_table.id = temp.id COLLATE SQL_Latin1_General_CP1_CI_AS
        AND my_table.key = 'SOME STRING'



回答2:


Specify the collation inside the declaration of your temp table.

CREATE TABLE #TempTable (ID NVARCHAR(255) COLLATE database_default)



回答3:


The problem is the temp table. It uses the collation of the tempdb.

You could create a table in your actual db and not a temp table and then they would have the same collation. Or specify collation upon creating temp table.




回答4:


try

SELECT * FROM my_table
INNER JOIN #TempTable temp    
    ON my_table.id = temp.id collate database_default
    AND my_table.key = 'SOME STRING'


来源:https://stackoverflow.com/questions/1404880/sql-collation-conflict-when-comparing-to-a-column-in-a-temp-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!