Vamos a ver donde esta el problema:
Creamos 1 tabla
El collate por default de mi servidor es: Modern_Spanish_CI_AS
Código SQL:
Ver originalCREATE TABLE testing
(
valor1 VARCHAR(20),
valor2 VARCHAR(20),
valor3 VARCHAR(20)
)
--Ejecutamos algunas veces este query:
INSERT INTO testing VALUES ('AAAAA','BBBB','CCCC')
Ahora creamos 3 tablas para usar diferentes collations:
Código SQL:
Ver originalSELECT VALUE = valor1, value2=valor2, value3=valor3 COLLATE Modern_Spanish_CI_AS INTO collate_1 FROM testing
SELECT VALUE = valor1, value2=valor2, value3=valor3 COLLATE SQL_Latin1_General_CP1_CI_AS INTO collate_2 FROM testing
SELECT VALUE = valor1, value2=valor2, value3=valor3 COLLATE SQL_Latin1_General_CP1_CI_AS INTO collate_3 FROM testing
Hacemos un query con un join:
Código SQL:
Ver originalSELECT t1.VALUE ,t1.value2 ,t1.value3 , t2.VALUE , t2.value2 , t2.value3
FROM collate_1 AS t1
INNER JOIN collate_3 AS t2 ON (t1.VALUE = t2.VALUE )
UNION ALL
SELECT t1.VALUE,t1.value2,t1.value3, t2.VALUE, t2.value2, t2.value3 FROM collate_2 AS t1
INNER JOIN collate_3 AS t2 ON (t1.VALUE=t2.VALUE)
y nos manda el error:
Mens. 457, Nivel 16, Estado 1, Línea 1
No se puede realizar la conversión implícita del valor varchar a varchar porque la intercalación del valor no está resuelta a causa de un conflicto de intercalación entre "SQL_Latin1_General_CP1_CI_AS" y "Modern_Spanish_CI_AS" en el operador UNION ALL.
ahora hacemos un query usando collate:
Código SQL:
Ver originalSELECT t1.VALUE ,t1.value2 ,t1.value3 , t2.VALUE , t2.value2 , t2.value3 COLLATE DATABASE_DEFAULT
FROM collate_1 AS t1
INNER JOIN collate_3 AS t2 ON (t1.VALUE = t2.VALUE )
UNION ALL
SELECT t1.VALUE,t1.value2,t1.value3, t2.VALUE, t2.value2, t2.value3 FROM collate_2 AS t1
INNER JOIN collate_3 AS t2 ON (t1.VALUE=t2.VALUE)
Nos vuelve a mandar error.......Verificamos el collate de nuestras tablas:
Código SQL:
Ver originalSELECT
col.name, col.collation_name
FROM
sys.COLUMNS col
WHERE
object_id = OBJECT_ID('collate_1')
--value Modern_Spanish_CI_AS
--value2 Modern_Spanish_CI_AS
--value3 Modern_Spanish_CI_AS
SELECT
col.name, col.collation_name
FROM
sys.COLUMNS col
WHERE
object_id = OBJECT_ID('collate_2')
--value Modern_Spanish_CI_AS
--value2 Modern_Spanish_CI_AS
--value3 SQL_Latin1_General_CP1_CI_AS
SELECT
col.name, col.collation_name
FROM
sys.COLUMNS col
WHERE
object_id = OBJECT_ID('collate_3')
--value Modern_Spanish_CI_AS
--value2 Modern_Spanish_CI_AS
--value3 SQL_Latin1_General_CP1_CI_AS
Y vemos que en 2 de ellas tenemos un collate diferente, para eso usamos el siguiente query:
Código SQL:
Ver originalSELECT t1.VALUE ,t1.value2 ,t1.value3 COLLATE database_default, t2.VALUE , t2.value2 , t2.value3 COLLATE DATABASE_DEFAULT
FROM collate_1 AS t1
INNER JOIN collate_3 AS t2 ON (t1.VALUE = t2.VALUE )
UNION ALL
SELECT t1.VALUE,t1.value2,t1.value3, t2.VALUE, t2.value2, t2.value3 FROM collate_2 AS t1
INNER JOIN collate_3 AS t2 ON (t1.VALUE=t2.VALUE)
Y nos regresa el resultado :), aqui lo que hay que tomar en cuenta son las columnas con collate diferente, revisa las de tu base de datos y ve cuales necesitas incluir en tu query :)
P.D: Tambien se puede usar en lugar del collate database_default el collate correspondiente en mi caso Modern_Spanish_CI_AS