Question : collation issue when joining two tables  - SQL server 2005 TSql

I have a query that uses a TempTable. Then I use this temp table to join with another table that exists in teh database.
The database ( and the otehr table ) collation is
SQL_Latin1_General_CP850_CI_AS

when I run my query I get en error see below.
Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.

My temp table and the join query is appended in the code section below
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
CREATE TABLE #MyTempItems ( ItemCode NVARCHAR(20), 			Quantity  NUMERIC(16,6) , 					MonthNo NUMERIC (16,0)  ,					MonthNm NVARCHAR(20), 					YearNo NUMERIC (16,0), 					SubTotal NUMERIC (16,6),					DocDate DATETIME,
                                [RowKey] [numeric](18, 0) IDENTITY(1,1) NOT NULL  )
 
 
-- Join query 
SELECT O.ItemCode, T.Quantity, T.MonthNo, T.YearNo, T.SubTotal,
	T.DocDate FROM #MyTempItems T INNER JOIN Items O ON T.ItemCode = O.ItemCode
	ORDER BY T.ItemCode, T.YearNo,  T.MonthNo

Answer : collation issue when joining two tables  - SQL server 2005 TSql

Start by checking the default collation for the database.  I am willing to bet that it matches your temp table's collation. The other possiblity is that the Items table was created with a specified collation that doesn't match that of the database.

What you will probably need to do is to control the collation for your temp table(s) from now on (or, at least, until you can install a new instance of SQL Server and control its collation so that everything winds up with the same collation.)  

If your problem is that the temp table is of the wrong collation then the following will help:

  • The quick and dirty solution to that problem is to use SQL Server Management Studio to script out the Items table as a CREATE TABLE into a new query and then look at how it is setting the collation.   It should show up in the table's DDL.  If that doesn't work, right click on the database and select Tasks (I think that is the tag . . . I don't have it immediately avaialbe since I am at home ;-).  Anyway, go through the scripting process for the database, being sure to check the selection for scripting the collation, but only scritp the one table.
     
  • Once ou have a script that shows the collation, you can copy that and add it to your temp table CREATE statement and that should address the immediate problem.   

If your problem is that your Items table is the one with the wrong collation, then you should check all of the other non-temp tables because they may also be of the same collation as the Items table.  

If the Items table is the only one out of collation, then you can try recreating that table with the default collation.  To do that, script out the table (wihtout the collation) and then alter the name of the table that will be created (so it doesn't "collide" with the Items table).  Once that is done, insert all of the data from the Items table into the New_Items.  You then need to drop the Items table, rename the New_Items table, set up all of the previously existing indexes and constraints, and make sure everything links up properly.

Your bigger problem is that you apparently have a database instance that has one collation and at least one table that doesn't match that collation.  It is entirely possible that every non-temp table is not using a collation matching the database.  If this is true, then you need to scrip out the entir database (without any collations), including all aspects (triggers, stored procs, constraints, indexes, etc.), probably install a new instance of SQL Server (because the instance may be an issue as far as collations), and execute the script to create a completely new database.  Once that is done, you can link the 2 databases and use INSERT INTO . . . SELECT FROM . . . to migrate the data or you can run an SSIS script to copy the data.

Random Solutions  
 
programming4us programming4us