Cannot resolve the collation conflict

坚强是说给别人听的谎言 提交于 2019-12-10 15:51:56

问题


I am getting the following error message.

Cannot resolve the collation conflict between "Latin1_General_CI_AI" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.

I only get it when I place this code below in my WHERE clause.

WHERE Region IN (SELECT Token FROM dbo.getParmsFromString(@Region))

Now @Region contains all the values from my multi-select fields from SSRS.

Below is the code for the function that is used.

CREATE FUNCTION [dbo].[getParmsFromString]
    (@String VARCHAR(MAX))
RETURNS @Parms TABLE
(
    Token VARCHAR(MAX)
)
AS
BEGIN
    IF CHARINDEX(',', @String) != 0
    BEGIN
        ;WITH cte0(Token, List) AS
              (
                SELECT   SUBSTRING(@String, 1, CHARINDEX(',',@String,1) - 1)
                        ,SUBSTRING(@String,CHARINDEX(',',@String,1) + 1, LEN(@String)) + ','

                UNION ALL

                SELECT     SUBSTRING(List,1,ISNULL(CHARINDEX(',',List,1) - 1,1))
                        ,SUBSTRING(List,CHARINDEX(',',List,1) + 1, LEN(List))
                FROM cte0
                WHERE LEN(cte0.List) > 0
              )

            INSERT INTO @Parms (Token)
            SELECT Token
            FROM cte0
            OPTION (MAXRECURSION 0)
        RETURN;
    END

    ELSE
        INSERT INTO @Parms
            SELECT @String
        RETURN;
END

回答1:


Try changing

 RETURNS @Parms TABLE 
( 
    Token VARCHAR(MAX) 
) 

with

try changing RETURNS @Parms TABLE 
( 
    Token VARCHAR(MAX) COLLATE DATABASE_DEFAULT
)  

and

WHERE Region IN (SELECT Token FROM dbo.getParmsFromString(@Region))   

with

WHERE Region COLLATE DATABASE_DEFAULT  IN (SELECT Token FROM dbo.getParmsFromString(@Region))  



回答2:


You definitly should check out this link:

http://msdn.microsoft.com/de-de/library/ms179886.aspx

There is also an example how to change the collation in a query.




回答3:


Generally this type of error occurs when you try to compare the the data of different regions or when you compare data using a specific encryption with other data using a different encryption. The most probable reason is that their tempdb is using the collation "SQL_Latin1_General_CP1_CI_AS" while the database is using "Latin1_General_CI_AS". As a result, temp objects are created under the collation "SQL_Latin1_General_CP1_CI_AS" and then fail to compare with database objects of the database which are using the collation "Latin1_General_CI_AS".

The easiest fix and also the one which would recommended would be to run the database on a server which was installed using collation "Latin1_General_CI_AS".

FYI. SQL collations("SQL_Latin1_General_CP1_CI_AS") are present in sql server for backward compatibility. When dealing with international data or databases using unicode and non unicode data, it is recommended to use windows collations ("Latin1_General_CI_AS").

You can change your database collation by:

use master
ALTER DATABASE "Your database"
COLLATE Latin1_General_CI_AS;

SELECT name, collation_name
FROM sys.databases;

and if needed you can also change the collation of "master" database i.e. rebuilding the database, for this go through to these links:

http://msdn.microsoft.com/en-us/library/dd207003(v=sql.100).aspx

http://sqlbuzz.wordpress.com/2011/08/20/how-to-rebuild-master-database-aka-rebuilding-sql-server-2008r2/

but make sure you backup all your database before doing this.



来源:https://stackoverflow.com/questions/12639163/cannot-resolve-the-collation-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!