首先,更改数据库排序规则不会影响表中现有的列,但会影响新创建的表和列。这是在数据库中创建新对象时使用的默认排序规则。
因此,您要做的就是查找所有现有列并更新当前排序规则。
您可以使用 sql 游标并查询所有表和列并更新特定类型或列名的排序规则。
举例:
1)您更改未来对象的排序规则
USE master;
GO
ALTER DATABASE databasename COLLATE French_CI_AI ;
GO
2)手动更改特定列的列排序规则
ALTER TABLE tablenameX ALTER COLUMN LastName varchar(100) COLLATE French_CI_AI NULL
ALTER TABLE tablenameY ALTER COLUMN FirstName varchar(100) COLLATE French_CI_AI NULL
3) 生成带光标的sql脚本
您需要决定更改排序规则的列。它可以是类型、名称或特定表。
您可以从查看您拥有的字符类型开始并在示例中替换它们,请参见列光标。
从 information_schema.columns 中选择不同的 DATA_TYPE
你可以参考这篇文章:
http://www.codeproject.com/Articles/302405/The-Easy-way-of-sharing-Collation-of-all-Database
declare @CollationName as nvarchar = 'Latin1_General_CI_AS'
declare @tablename as nvarchar(100) = ''
declare @sqltext as nvarchar(100) = ''
declare @columnname as nvarchar(200) = ''
declare @DataType as nvarchar(100) = ''
declare @CharacterMaxLen as int = 0
declare @IsNullable as bit = 0
DECLARE MyTableCursor Cursor
FOR
SELECT * from information_schema.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
OPEN MyTableCursor
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE MyColumnCursor Cursor
FOR
SELECT COLUMN_NAME,DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
IS_NULLABLE from information_schema.columns
WHERE table_name = @TableName AND (Data_Type LIKE '%char%'
OR Data_Type LIKE '%text%') AND COLLATION_NAME <> @CollationName
ORDER BY ordinal_position
Open MyColumnCursor
FETCH NEXT FROM MyColumnCursor INTO @ColumnName, @DataType,
@CharacterMaxLen, @IsNullable
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLText = 'ALTER TABLE ' + @TableName + ' ALTER COLUMN [' + @ColumnName + '] ' +
@DataType + '(' + CASE WHEN @CharacterMaxLen = -1 THEN 'MAX' ELSE @CharacterMaxLen END +
') COLLATE ' + @CollationName + ' ' +
CASE WHEN @IsNullable = 'NO' THEN 'NOT NULL' ELSE 'NULL' END
PRINT @SQLText
FETCH NEXT FROM MyColumnCursor INTO @ColumnName, @DataType,
@CharacterMaxLen, @IsNullable
END
CLOSE MyColumnCursor
DEALLOCATE MyColumnCursor
FETCH NEXT FROM MyTableCursor INTO @TableName
END
CLOSE MyTableCursor
DEALLOCATE MyTableCursor