Sql Server - Using Collation - sql-server

I am using below query in which I need to specify collation hint to avoid collation issues across databases as this query uses tables from 2 databases.
Msg 468, Level 16, State 9, Line 12 Cannot resolve the collation
conflict between "Latin1_General_CS_AI" and "Latin1_General_CS_AS" in
the equal to operation.
Currently I am getting above error for collation conflicts when I run some of the queries which uses different databases with different collations:
Delete from table1 where oldcolumn in
(
select newcolumn from Database2.dbo.table2
where invoiceid = #invno
and complete = 0
)
I changed the query to include collation hint as below:
Delete from table1 where oldcolumn COLLATE SQL_Latin1_General_CP1_CS_AS in
(
select newcolumn from Database2.dbo.table2
where invoiceid = #invno
and complete = 0
)
Will above query solve the problem of collation?
Is it same to specify collate hint on left or right of operator (e.g. "=" operator)?
Can query like invoiceid = #invno ever generate runtime collation conflit error?
Note: I am asking this question as I do not have access to any of the above 2 databases and the script will be run on actual databases.

Use Below Query :
DELETE FROM Table1
WHERE Table1.ID IN (
SELECT Table1.ID
FROM Table1
INNER JOIN Database2.dbo.Table2 Table2 ON Table2.NewColumn = Table1.OldColumn COLLATE SQL_Latin1_General_CP1_CS_AS
WHERE Table2.invoiceid = #invno
AND Table2.complete = 0
)

Related

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

I am trying to create a procedure in SQL Server 2008R2 but it is showing this error
Cannot resolve the collation conflict between "Latin1_General_CI_AI"
and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
The procedure that I have created is
CREATE Procedure Ps_Quiz_OnlineTest_QuestionsWithOptions_Get --'Ques1'
#Ques Varchar(8000)
As
Begin
Select
A.QuestionId,
A.QsnDesc,
A.CorrectOption,
B.OptionValue,
A.Marks,
(
Select QsnName
From Quiz_tblQsnsLimitMaster
Where QsnId = #Ques) QuesPaper,
(
Select Durationoftest
From Quiz_tblQsnsLimitMaster
Where QsnId = #Ques) QuesPaper
From
Quiz_tblQsnCreationMaster A,
Quiz_tblQsnCreationDetail B
Where
A.QuestionId = B.QuestionId
And A.QuestionId In (
Select QuestionIds
From FN_Question_Answers_Quiz(#Ques))
And B.QuestionId In (
Select QuestionIds
From FN_Question_Answers_Quiz(#Ques))
Order By
A.QuestionId,
B.OptionOrder
End
I tried to collate tables with different collations but it did not worked.
How can I solve this.
probably this might help, change your where condition like this
Where A.QuestionId COLLATE DATABASE_DEFAULT = B.QuestionId COLLATE DATABASE_DEFAULT
Since you've mentioned that QuestionID in each table is varchar, it's probably the comparison between those. So at a guess, try changing the line:
Where A.QuestionId = B.QuestionId And
To the following:
Where A.QuestionId = B.QuestionId COLLATE SQL_Latin1_General_CP1_CI_AS And
I'd prefer it if you used proper join syntax for your tables - but that's for another day ;-)
try use this,
where fieldname COLLATE DATABASE_DEFAULT = secondfieldname COLLATE DATABASE_DEFAULT
this is working
Just use the following syntax when joining tables with different collations,
where A.QuestionId collate SQL_Latin1_General_CP1_CI__AS = B.QuestionId collate
SQL_Latin1_General1_General_CP1_CI_AS
To overcome this use the below code:
SELECT NAME
FROM sys.objects
WHERE NAME COLLATE DATABASE_DEFAULT NOT IN (SELECT TYPE
FROM sys.objects)
I was facing same issue after migration of Database and shifting of server by service provider. So as solution I have created new database using CPanel then execute SQL query for schema and data to restore. Finally this issue got resolved.

Collation conflict with temp table

The collation of tempdb is Latin1_General_100_CI_AI. The collation of the database is also Latin1_General_100_CI_AI. Yet the following SQL statement:
SELECT *
FROM ##CitiesMapping AS cm
INNER JOIN Cities ON cm.CityName=Cities.Name
returns:
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AI" in the equal to operation.
The server default collation is also Latin1_General_100_CI_AI
It is possible that the collation is set differently for a single column. The query from Stuart will show you that. If they are different collations you can specify the collation being used on either side of the comparison like this:
SELECT *
FROM ##CitiesMapping AS cm
INNER JOIN Cities
ON cm.CityName COLLATE DATABASE_DEFAULT = Cities.Name COLLATE DATABASE_DEFAULT;
I hope this helps you out.
Check the tables involved as well:
SELECT name, collation_name, OBJECT_NAME(object_id)
FROM sys.columns
WHERE OBJECT_NAME(object_id) IN ('Cities')

Unable to Increase Column size

I need increase column size of the table , i am using below query to increase the size but i am getting below error
Alter Table Tabl1 Alter Column Col1 VarChar(6) Not NULL
Msg 5074, Level 16, State 1, Line 1
The object 'Tabl1' is dependent on column 'Col1'.
Msg 5074, Level 16, State 1, Line 1
The statistics '_WA_Sys_Col1_5070F446' is dependent on column 'Col1'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN Col1 failed because one
or more objects access this column.
Because of same table as a dependency on the column
need help on this
SQL Server automatically adds statistics to a table over time to use when it parses a query and builds a query plan. You have to drop the statistic to change the column. For instance:
drop statistics [dbo].[Tabl1].[_WA_Sys_Col1_5070F446]
However, you should use SSMS to view the columns that are in the _WA_Sys_Col1_5070F446 statistics before you drop it so that you can recreate it. Something like this:
create statistics [_WA_Sys_Col1_5070F446] on [dbo].[Tabl1]([Col1])
But there may be more columns..., so be sure to find out which need to be included before you drop it.
You can run this SQL to find most of the dependencies, it doesn't report the statistics dependencies, but it catches most of the others:
SELECT OBJECT_NAME(d.object_id) AS SP_Or_Function, OBJECT_NAME(d.referenced_major_id) AS TableReferenced
FROM sys.sql_dependencies AS d INNER JOIN
sys.all_sql_modules AS m ON m.object_id = d.object_id
where OBJECT_ID(N'Tabl1') = d.referenced_major_id
GROUP BY OBJECT_NAME(d.object_id), OBJECT_NAME(d.referenced_major_id)
ORDER BY SP_Or_Function, TableReferenced
You can find all statistics used by a given table with this query:
SELECT DISTINCT
OBJECT_NAME(s.[object_id]) AS TableName,
c.name AS ColumnName,
s.name AS StatName,
s.auto_created,
s.user_created,
s.no_recompute,
s.[object_id],
s.stats_id,
sc.stats_column_id,
sc.column_id,
STATS_DATE(s.[object_id], s.stats_id) AS LastUpdated
FROM sys.stats s JOIN sys.stats_columns sc ON sc.[object_id] = s.[object_id] AND sc.stats_id = s.stats_id
JOIN sys.columns c ON c.[object_id] = sc.[object_id] AND c.column_id = sc.column_id
JOIN sys.partitions par ON par.[object_id] = s.[object_id]
JOIN sys.objects obj ON par.[object_id] = obj.[object_id]
WHERE OBJECTPROPERTY(s.OBJECT_ID,'IsUserTable') = 1
AND (s.auto_created = 1 OR s.user_created = 1)
AND object_id(N'Tabl1') = s.[object_id]
Thanks to SQLAuthority for the last two SQL queries:
SQL SERVER – Get the List of Object Dependencies – sp_depends and information_schema.routines and sys.dm_sql_referencing_entities (Gabriel's post)
SQL SERVER – Find Details for Statistics of Whole Database – DMV – T-SQL Script
Here is quote from SQL Server 2000 help:
ALTER COLUMN
The altered column cannot be:
.....
Used in an index, unless the column is a varchar, nvarchar, or varbinary data type, the data type is not changed, and the new size is
equal to or larger than the old size.
Used in statistics generated by the CREATE STATISTICS statement. First remove the statistics using the DROP STATISTICS statement.
Statistics automatically generated by the query optimizer are
automatically dropped by ALTER COLUMN. .....

SQL change field Collation in a select

i'm trying to do the following select:
select * from urlpath where substring(urlpathpath, 3, len(urlpathpath))
not in (select accessuserpassword from accessuser where accessuserparentid = 257)
I get the error:
Cannot resolve the collation conflict between
"SQL_Latin1_General_CP1_CI_AI" and
"SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Does anyone know how i can cast as a collation, or something that permits me to match this condition?
Thanx
You can add COLLATE CollationName after the column name for the column you want to "re-collate". (Note: the collation name is literal, not quoted)
You can even do the collate on the query to create a new table with that query, for example:
SELECT
*
INTO
#TempTable
FROM
View_total
WHERE
YEAR(ValidFrom) <= 2007
AND YEAR(ValidTo)>= 2007
AND Id_Product = '001'
AND ProductLine COLLATE DATABASE_DEFAULT IN (SELECT Product FROM #TempAUX)
COLLATE DATABASE_DEFAULT causes the COLLATE clause inherits the collation of the current database, eliminating the difference between the two

Collation conflict SQL Server 2008

I've been going around this but I haven't found a solution for my problem. My sql query is:
SELECT
dbo.Country.CtyRecID, dbo.Country.CtyShort, dbo.Notification.NotRecID,
dbo.Notification.NotName, dbo.TemporalSuspension.TCtsCode,
dbo.TemporalSuspension.TCtsCodeRecID,
dbo.TaxPhylum.PhyName AS Taxon, dbo.TemporalSuspension.TCtsNotes,
dbo.TemporalSuspension.TCtsRecID,
dbo.TemporalSuspension.TCtsKgmRecID,
CASE dbo.TemporalSuspension.TCtsKgmRecID WHEN 1 THEN 'Animals'
WHEN 2 THEN 'Plants' ELSE 'All' END AS Kingdom
FROM
dbo.TemporalSuspension
INNER JOIN dbo.Notification
ON dbo.TemporalSuspension.TCtsStartNotRecID = dbo.Notification.NotRecID
INNER JOIN dbo.Country
ON dbo.TemporalSuspension.TCtsCtyRecID = dbo.Country.CtyRecID
INNER JOIN dbo.TaxPhylum
ON dbo.TemporalSuspension.TCtsCodeRecID = dbo.TaxPhylum.PhyRecID
AND dbo.TemporalSuspension.TCtsCode LIKE 'PHY'
UNION ALL
SELECT
dbo.Country.CtyRecID, dbo.Country.CtyShort, dbo.Notification.NotRecID,
dbo.Notification.NotName, dbo.TemporalSuspension.TCtsCode,
dbo.TemporalSuspension.TCtsCodeRecID,
dbo.TaxClass.ClaName AS Taxon, dbo.TemporalSuspension.TCtsNotes,
dbo.TemporalSuspension.TCtsRecID,
dbo.TemporalSuspension.TCtsKgmRecID,
CASE dbo.TemporalSuspension.TCtsKgmRecID WHEN 1 THEN 'Animals'
WHEN 2 THEN 'Plants' ELSE 'All' END AS Kingdom
FROM
dbo.TemporalSuspension
INNER JOIN dbo.Notification
ON dbo.TemporalSuspension.TCtsStartNotRecID = dbo.Notification.NotRecID
INNER JOIN dbo.Country
ON dbo.TemporalSuspension.TCtsCtyRecID = dbo.Country.CtyRecID
INNER JOIN dbo.TaxClass
ON dbo.TemporalSuspension.TCtsCodeRecID = dbo.TaxClass.ClaRecID
AND dbo.TemporalSuspension.TCtsCode LIKE 'CLA'
But I don't understand why it doesn't work, I keep getting this error :
Cannot resolve collation conflict for column 7 in SELECT statement.
What's wrong? I've used this other times and I never got this problem. According to the error the dbo.TaxPhylum.PhyName AS Taxon, and dbo.TaxClass.ClaName AS Taxon, is the thing giving the problem, but I don't really understand why, both columns have the same type and everything.
EDIT: This is the result obtained with the query, how do I get around this?
Column Name Table Name collation_name
PhyName vDecisionsExpanded Latin1_General_CI_AS
ClaName vDecisionsExpanded SQL_Latin1_General_CP1_CI_AS
thanks
Try this query in your database:
SELECT
col.name 'Column Name',
OBJECT_NAME(object_id) 'Table Name',
col.collation_name
FROM sys.columns col
WHERE col.system_type_id IN (35, 99, 167, 175, 231, 239) -- TEXT, NTEXT, VARCHAR etc.
It will show you all string-related columns in your database, and their collation.
The error message says that column 7 is the culprit - that would be dbo.TaxPhylum.PhyName - so also check the TaxPhylum database. Is the collation in that database different from your normal database??
UPDATE:
if you have a collation conflict, you can do two things:
1) if it's only a single or a few columns in a SELECT, just add the COLLATE ..... modifier to them:
SELECT
.....
dbo.TaxPhylum.PhyName COLLATE SQL_Latin1_General_CP1_CI_AS AS Taxon,
.....
2) if it's a lot of columns, you might want to consider to modify the COLLATION on those columns / tables or in that database all together
How to change database or server collation
Different server/database combination? At the risk of asking the obvious, I assume that you have verified your columns collation sequences?

Resources