SQL_Database ve Table Collation name değiştirme ( Alter Collation Name)
<p>ALTER DATABASE SManagement SET SINGLE_USER WITH ROLLBACK IMMEDIATE<br /> ALTER DATABASE SManagement COLLATE SQL_Latin1_General_CP1_CI_AS<br /> ALTER DATABASE SManagement SET MULTI_USER</p> <p> </p> <p> </p> <blockquote> <p>DECLARE @collate nvarchar(100);<br /> DECLARE @table nvarchar(255);<br /> DECLARE @column_name nvarchar(255);<br /> DECLARE @column_id int;<br /> DECLARE @data_type nvarchar(255);<br /> DECLARE @max_length int;<br /> DECLARE @row_id int;<br /> DECLARE @sql nvarchar(max);<br /> DECLARE @sql_column nvarchar(max);</p> <p>SET @collate = 'SQL_Latin1_General_CP1_CI_AS';</p> <p>DECLARE local_table_cursor CURSOR FOR</p> <p>SELECT [name]<br /> FROM sysobjects<br /> WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1</p> <p>OPEN local_table_cursor<br /> FETCH NEXT FROM local_table_cursor<br /> INTO @table</p> <p>WHILE @@FETCH_STATUS = 0<br /> BEGIN</p> <p> DECLARE local_change_cursor CURSOR FOR</p> <p> SELECT ROW_NUMBER() OVER (ORDER BY c.column_id) AS row_id<br /> , c.name column_name<br /> , t.Name data_type<br /> , c.max_length<br /> , c.column_id<br /> FROM sys.columns c<br /> JOIN sys.types t ON c.system_type_id = t.system_type_id<br /> LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id<br /> LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id<br /> WHERE c.object_id = OBJECT_ID(@table)<br /> ORDER BY c.column_id</p> <p> OPEN local_change_cursor<br /> FETCH NEXT FROM local_change_cursor<br /> INTO @row_id, @column_name, @data_type, @max_length, @column_id</p> <p> WHILE @@FETCH_STATUS = 0<br /> BEGIN</p> <p> IF (@max_length = -1) OR (@max_length > 4000) SET @max_length = 4000;</p> <p> IF (@data_type LIKE '%char%')<br /> BEGIN TRY<br /> SET @sql = 'ALTER TABLE ' + @table + ' ALTER COLUMN ' + @column_name + ' ' + @data_type + '(' + CAST(@max_length AS nvarchar(100)) + ') COLLATE ' + @collate<br /> PRINT @sql<br /> EXEC sp_executesql @sql<br /> END TRY<br /> BEGIN CATCH<br /> PRINT 'ERROR: Some index or constraint rely on the column' + @column_name + '. No conversion possible.'<br /> PRINT @sql<br /> END CATCH</p> <p> FETCH NEXT FROM local_change_cursor<br /> INTO @row_id, @column_name, @data_type, @max_length, @column_id</p> <p> END</p> <p> CLOSE local_change_cursor<br /> DEALLOCATE local_change_cursor</p> <p> FETCH NEXT FROM local_table_cursor<br /> INTO @table</p> <p>END</p> <p>CLOSE local_table_cursor<br /> DEALLOCATE local_table_cursor</p> <p>GO</p> </blockquote>
0 Yorum