ฉันทำอะไรแบบนี้และมันใช้งานได้ แต่คุณต้องจำดัชนีที่ชี้ไปที่ชนิดข้อมูลเป็น text / varchar / nvarchar ต้องถูกดร็อปรันสคริปต์แล้วสร้างดัชนี
USE YourDataBase
GO
DECLARE @Table_Name NVARCHAR(100)
SET @Table_Name = NULL--- THIS IS THE TableName that you want to change its collation columns
--- if null will set to all tables
DECLARE @TempTable AS TABLE
(
ID INT IDENTITY
,TableName NVARCHAR(100)
,ColumnName NVARCHAR(100)
,TypeName NVARCHAR(100)
,Max_length INT
,Collation_Name NVARCHAR(100)
,EnterDtm DATETIME DEFAULT GETDATE()
)
DECLARE @NewCollation NVARCHAR(100)
SET @NewCollation = 'Latin1_General_CI_AS' --- THIS IS THE COLLATION NAME THAT YOU WANT TO CHANGE
INSERT INTO @TempTable(TableName,ColumnName,TypeName,Max_length,Collation_Name)
SELECT
QUOTENAME(SCHEMA_NAME(tables.schema_id)) + '.' + QUOTENAME(tables.name) AS TableName
,all_columns.name AS ColumnName
,type_name(all_columns.user_type_id)
,all_columns.max_length
,all_columns.collation_name
from sys.all_columns INNER JOIN sys.tables ON
tables.object_id = all_columns.object_id
AND collation_name IS NOT NULL
AND all_columns.collation_name != @NewCollation
WHERE tables.object_id = ISNULL(object_id(@Table_Name),all_columns.object_id)
DECLARE @TableID SMALLINT
SET @TableID = (SELECT MIN(ID) FROM @TempTable)
DECLARE @Query NVARCHAR(1000),@TableName NVARCHAR(100),@ColumnName NVARCHAR(100),@TypeName NVARCHAR(100)
,@Size INT
WHILE @TableID IS NOT NULL
BEGIN
SET @TableName = (SELECT TableName FROM @TempTable WHERE ID = @TableID)
SET @ColumnName = (SELECT QUOTENAME(ColumnName) FROM @TempTable WHERE ID = @TableID)
SET @TypeName = (SELECT TypeName FROM @TempTable WHERE ID = @TableID)
SET @Size = (SELECT Max_length FROM @TempTable WHERE ID = @TableID)
SET @Query='ALTER TABLE ' + @TableName + ' ALTER COLUMN ' + @ColumnName + ' ' + @TypeName+ ISNULL ('(' +CAST(@Size AS VARCHAR(200))+')', '') +' COLLATE '+ @NewCollation
PRINT (@Query)
SET @TableID = (SELECT MIN(ID) FROM @TempTable WHERE ID > @TableID)
END
นี่เป็นคำตอบแรกของฉันที่โพสต์ให้อภัยฉัน