Cannot resolve the collation conflict between “SQL

2019-03-01 13:54发布

问题:

I have a piece of code in my stored procedure as below -

update tblexpwitretrocmdocs set sCheckedOut = A.sEditor, idone = 0 
from #tblDocs A
JOIN tblexpwitretrocmdocs B ON A.SID = B.SID
where A.iDocumentTypeId in (16,17,13,11)
and A.sid not in (select SID COLLATE SQL_AltDiction_CP850_CI_AS from tblexpwitdocumentgeneral)

I am getting the error "Cannot resolve the collation conflict between "SQL_AltDiction_CP850_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation." for the first line of code.

The column - sCheckedOut in table - tblexpwitretrocmdocs has collation SQL_AltDiction_CP850_CI_AS. So to make the column - sEditor compatible to it, I defined the temp table as below -

CREATE TABLE #tblDocs(
iId INT IDENTITY (1,1),
SID NVARCHAR(50) COLLATE SQL_AltDiction_CP850_CI_AS,
iDocumentTypeId INT,
sType NVARCHAR(200),
sEditor NVARCHAR(50) COLLATE SQL_AltDiction_CP850_CI_AS 
)

Still I am getting the same error. Please help me resolve this.

回答1:

To resolve the collation conflict add "COLLATE DATABASE_DEFAULT" keywords around “=” operator as shown below:

UPDATE tblexpwitretrocmdocs SET sCheckedOut = A.sEditor, idone = 0 
FROM #tblDocs A
JOIN tblexpwitretrocmdocs B ON A.SID = B.SID
WHERE A.iDocumentTypeId in (16,17,13,11) COLLATE DATABASE_DEFAULT
AND A.sid COLLATE DATABASE_DEFAULT NOT IN 
(SELECT SID COLLATE DATABASE_DEFAULT FROM tblexpwitdocumentgeneral)

Hope this helps...



回答2:

Specify DATABASE_DEFAULT for the collation of all temp table string columns to use the current database default collation:

CREATE TABLE #tblDocs(
    iId INT IDENTITY (1,1),
    SID NVARCHAR(50) COLLATE DATABASE_DEFAULT,
    iDocumentTypeId INT,
    sType NVARCHAR(200) COLLATE DATABASE_DEFAULT,
    sEditor NVARCHAR(50) COLLATE DATABASE_DEFAULT
);

For columns that differ from the database default collation, specify the exact column collation instead of DATABASE_DEFAULT.