Cannot resolve the collation conflict while joining tables against temp tables on SQL Server

When writing long query’s or stored procedures using temporary tables, you may run into an error like:

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

The cause of this error is that the tables you are querying against have a different collation then the server default. This causes the temporary tables in TempDb.. to have a different collation too.

To specify the collation your database has during the creation of your temptable, implemenet it like this:

-- Clear out temp. table if it already exists
begin try
	drop table #sometable
end try
begin catch
end catch

-- Create with enforced collation
create table #sometable (
field1 nvarchar(50) collate Latin1_General_CI_AS,
field2 nvarchar(50) collate Latin1_General_CI_AS,
.....
)

Leave a Reply

*