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

后端 未结 4 1105
遇见更好的自我
遇见更好的自我 2020-12-31 10:42

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         


        
相关标签:
4条回答
  • 2020-12-31 11:15

    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.

    0 讨论(0)
  • 2020-12-31 11:21

    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'
    
    0 讨论(0)
  • 2020-12-31 11:37

    try

    SELECT * FROM my_table
    INNER JOIN #TempTable temp    
        ON my_table.id = temp.id collate database_default
        AND my_table.key = 'SOME STRING'
    
    0 讨论(0)
  • 2020-12-31 11:40

    Specify the collation inside the declaration of your temp table.

    CREATE TABLE #TempTable (ID NVARCHAR(255) COLLATE database_default)
    
    0 讨论(0)
提交回复
热议问题