You may encounter after the backup and restore SQL Server inconsistencies collation, this time can be solved by a unified collation. Detailed operations are as follows:
Database error message: Unable to resolve equal to operation in the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AS".
To solve the conflict collation, the collation can be changed directly corresponding to the field to match the query mistakes can be avoided, as follows: the ALTER TABLE [table] ALTER COLUMN [field name] nvarchar (256) COLLATE Chinese_PRC_CI_AS '
But there are many database ordered as "SQL_Latin1_General_CP1_CI_AS" field, if one by one to change, then a few dozen fields can also be considered, if several hundreds workload can be imagined. We can first query fields currently required to modify the database, query the corresponding table names, field names, collation, field types, as well as the corresponding length, etc., as follows:
SELECT t.name AS [Table], c.name AS [Column], c.collation_name AS [Collation], TYPE_NAME( c.system_type_id) AS [TypeName], c.max_length AS [TypeLength] FROM sys.columns c RIGHT JOIN sys.tables t ON c.object_id = t.object_id WHERE c.collation_name IS NOT NULL
Performing the above statement, you may find a few more lines, excessive amount of modification is basically impossible to manually modify slowly, you need to modify the unified through SQL query results. Blog friends recommend Park will result sets into a temporary table, by circulating a temporary table, exec execute SQL statements to modify the splicing of each record, the specific code as follows:
DECLARE @table NVARCHAR (128) - cyclic Item table DECLARE @column NVARCHAR (128) - cyclic Item field name DECLARE @type NVARCHAR (128) - the type of the corresponding field, char, nchar, varchar, nvarchar like DECLARE @ typeLenght nVARCHAR (128) - the length of the corresponding type, nchar, nvarchar value needs to be divided by 2 the DECLARE @sql nVARCHAR (MAX) - SQL statement to be executed splicing the SET ROWCOUNT 0 the SELECT NULL MyKey, c.NAME, t.name the AS [the Table], c.NAME the AS [the Column], c.collation_name the AS [the Collation], type_name (c.system_type_id) the AS [the TypeName], c.max_length the AS [TypeLength] the INTO #temp the FROM C the sys.columns RIGHT the JOIN SYS T .tables the ON c.object_id = t.object_id the WHERE c.collation_name the IS the NOT NULL - Product table to test = T.name --and 'Product' the SET ROWCOUNT. 1 the UPDATE #temp the SET. 1 = MyKey the WHILE @@ ROWCOUNT> 0 the BEGIN the SET ROWCOUNT 0 - for each inquiry record and a first assigned to the corresponding variable SELECT @table = [ the Table], the @Column = [the Column], @type the TypeName =, @typeLenght = TypeLength the FROM #temp the WHERE = MyKey. 1 --nchar, in addition to the value required nvarchar 2 the IF the CONVERT (the INT, @typeLenght)> 0 the AND (@ = type 'nvarchar' @type = OR 'NCHAR') the BEGIN the SET @ = typeLenght the CONVERT (nVARCHAR (128), the CONVERT (the INT, @typeLenght) / 2) the END the IF @typeLenght = '-1' the BEGIN the SET typeLenght @ = ' max ' the END - splicing sql, Note that the table name, the field names with [], and the like to avoid Group Categories the SET @ SQL =' the ALTER TABLE [ '+ @table +'] ALTER COLUMN [ ' + @column + '] ' + @type + '(' + @typeLenght + ') COLLATE Chinese_PRC_CI_AS' --Try执行 BEGIN TRY EXEC(@sql) END TRY --Catch查询异常结果 BEGIN CATCH SELECT @sql AS [ASL], Error_message() AS msg END CATCH DELETE #temp WHERE mykey = 1 SET ROWCOUNT 1 UPDATE #temp SET mykey = 1 END SET ROWCOUNT 0 DROP TABLE #temp
Execute SQL, update errors try catch query results are displayed in a list, we can see that only a handful of several fields need to modify manually, these changes are mostly unsuccessful because of the associated foreign keys and so on, one by one investigation to . At this point, SQL automatically modify most of the field, greatly reducing the workload.
Thanks to the original author: https://www.cnblogs.com/Ken-Blogs/p/6676006.html