Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

collation

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

like image 873
Justin Avatar asked Sep 10 '09 12:09

Justin


People also ask

Can you set a collation for a column?

Database collation To apply new collation to existing tables of a database, the user can set new collation on TEXT columns of tables.

How do I create a temp table with collation in SQL?

DROP TABLE IF EXISTS #TempTable; CREATE TABLE #TempTable ( ProductCode VARCHAR(5) COLLATE DATABASE_DEFAULT NOT NULL, ProductDesc VARCHAR(200) NULL ); INSERT INTO #TempTable ( ProductCode,ProductDesc) SELECT ProductCode,ProductDesc FROM dbo. TestData; SELECT t. ProductCode, tt. ProductDesc FROM dbo.

Is SQL_Latin1_General_CP1_CI_AS the same as Latin1_General_CI_AS?

The SQL_Latin1_General_CP1_CI_AS collation is a SQL collation and the rules around sorting data for unicode and non-unicode data are different. The Latin1_General_CI_AS collation is a Windows collation and the rules around sorting unicode and non-unicode data are the same.

Does collation affect performance?

If you then specify a COLLATE clause in the query that is different than the collation used for the index, you will have a performance penalty because you won't be using that index.


1 Answers

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'
like image 138
Quassnoi Avatar answered Sep 22 '22 12:09

Quassnoi