I have 2 databases (X and Y) on my SQL Server 2008 R2. For some reason the databases and the server have different encodings (SQL_Latin1_general_CP1_CI_AS
and Latin1_General_CI_AS
) so I'm always facing collate problems when I use temp tables or table variables.
Selecting some info from database Y into the table variable @info
When I try to join both in this way it work really fine:
select d.data_id, i.info_id, i.info_value
from X.aaa.data d
join @info i on i.serial COLLATE DATABASE_DEFAULT =
SUBSTRING(d.serial, 17, 10) COLLATE DATABASE_DEFAULT
But the serial length can be different for different products so I want to do it with a variable:
declare @serial_length int = 10;
-- some switch case to set @serial_length to some other value
select d.data_id, i.info_id, i.info_value
from X.aaa.data d
join @info i on i.serial COLLATE DATABASE_DEFAULT =
SUBSTRING(d.serial, 17, @serial_length) COLLATE DATABASE_DEFAULT
When I try this the query will just run forever and do nothing.
Is there a workaround for this problem? Or does at least anyone know why this is happening?
UPDATE: Thanks to MitchWheat the collate isn't a problem any more Changing Database Collation and dealing with TempDB Objects
But the SELECT
still doesn't work. I'm using SUBSTRING(serial, 17, @serial_length)
also in another procedure but without table variables and there it works.