tags:

views:

332

answers:

4

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)...

+3  A: 

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'
Quassnoi
Thanks, I hadnt noticed that my id's were characters.
Kragen
A: 

Specify the collation inside the declaration of your temp table.

CREATE TABLE #TempTable (ID INT, [Key] NVARCHAR(255) COLLATE database_default)
Greco
A: 

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.

Daniel Elliott
Not true - depending on how the temp table was created, it would either take the collation of the database you are in, or the collation of tempdb - not master.If the temp table is explicitly created (CREATE TABLE #temp) then it takes the collation from tempdb.If the table is derived from another table (SELECT * INTO #temp from mytable) then it takes the collation from the source table.
Kev Riley
Good point ... as they are created at the same time I *wrongly* usually look at collation of master to find collation of tempdb. editted to incorporate correct answer. Ta :)
Daniel Elliott
A: 

try

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