Change collations of all columns of all tables in SQL Server

YvesR

I imported a database with some data to compare with another database.

The target database has collation Latin1_General_CI_AS and the source database has SQL_Latin1_General_CP1_CI_AS.

I did change the collation of the source database in general to Latin1_General_CI_AS using the SQL Server Management Studio. But the tables and columns inside remains with the old collation.

I know that I can change a column using:

ALTER TABLE [table] 
ALTER COLUMN [column] VARCHAR(100) COLLATE Latin1_General_CI_AS

But I have to do this for all tables and all columns inside.

Before I know start to write a stored procedure that reads all tables and inside all column of type varchar and change them in a table and column cursor loop...

Does anyone know an easier way or is the only way to do this with a script running through all tables in a procedure?

YvesR

As I did not find a proper way I wrote a script to do it and I'm sharing it here for those who need it. The script runs through all user tables and collects the columns. If the column type is any char type then it tries to convert it to the given collation.

Columns has to be index and constraint free for this to work.

If someone still has a better solution to this please post it!

DECLARE @collate nvarchar(100);
DECLARE @table nvarchar(255);
DECLARE @column_name nvarchar(255);
DECLARE @column_id int;
DECLARE @data_type nvarchar(255);
DECLARE @max_length int;
DECLARE @row_id int;
DECLARE @sql nvarchar(max);
DECLARE @sql_column nvarchar(max);

SET @collate = 'Latin1_General_CI_AS';

DECLARE local_table_cursor CURSOR FOR

SELECT [name]
FROM sysobjects
WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1

OPEN local_table_cursor
FETCH NEXT FROM local_table_cursor
INTO @table

WHILE @@FETCH_STATUS = 0
BEGIN

    DECLARE local_change_cursor CURSOR FOR

    SELECT ROW_NUMBER() OVER (ORDER BY c.column_id) AS row_id
        , c.name column_name
        , t.Name data_type
        , c.max_length
        , c.column_id
    FROM sys.columns c
    JOIN sys.types t ON c.system_type_id = t.system_type_id
    LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
    LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
    WHERE c.object_id = OBJECT_ID(@table)
    ORDER BY c.column_id

    OPEN local_change_cursor
    FETCH NEXT FROM local_change_cursor
    INTO @row_id, @column_name, @data_type, @max_length, @column_id

    WHILE @@FETCH_STATUS = 0
    BEGIN

        IF (@max_length = -1) OR (@max_length > 4000) SET @max_length = 4000;

        IF (@data_type LIKE '%char%')
        BEGIN TRY
            SET @sql = 'ALTER TABLE ' + @table + ' ALTER COLUMN ' + @column_name + ' ' + @data_type + '(' + CAST(@max_length AS nvarchar(100)) + ') COLLATE ' + @collate
            PRINT @sql
            EXEC sp_executesql @sql
        END TRY
        BEGIN CATCH
          PRINT 'ERROR: Some index or constraint rely on the column' + @column_name + '. No conversion possible.'
          PRINT @sql
        END CATCH

        FETCH NEXT FROM local_change_cursor
        INTO @row_id, @column_name, @data_type, @max_length, @column_id

    END

    CLOSE local_change_cursor
    DEALLOCATE local_change_cursor

    FETCH NEXT FROM local_table_cursor
    INTO @table

END

CLOSE local_table_cursor
DEALLOCATE local_table_cursor

GO

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Server: How to change collation of all columns of my select on the fly

分類Dev

SQL Search All Tables

分類Dev

Find all boolean columns from all tables

分類Dev

How to search a column name in all tables in a database in SQL Server 2012?

分類Dev

How to get the MAX() while NOT grouping all columns in SQL Server

分類Dev

SQL Server to check all columns in a databases views for a specific string value

分類Dev

Select all columns but change a column to string

分類Dev

Need to find out if all columns in a SQL Server table have the same value

分類Dev

Retaining all columns in both data tables during a join, then adding a column

分類Dev

DB2 SQL Select All With Columns As

分類Dev

Get all columns from an SQL Table without Computed Columns

分類Dev

SQL Server Sum Columns From Two Tables With Condition

分類Dev

SQL Server, selecting from 2 columns from different tables

分類Dev

Can not apply calculation to all rows in SQL Server

分類Dev

SQL Server all(select ...)がnull

分類Dev

How can I display all my sql tables in a ListBox

分類Dev

How to extract all tables (including where references) within a SQL query?

分類Dev

SQL - find all instances where two columns are the same

分類Dev

SQL: sum of all columns in only one query using UPDATE

分類Dev

Remove columns from SQL output where all values are 0

分類Dev

PL/SQL : ALL_TAB_COLUMNS.table_name%TYPE value

分類Dev

Query on all columns cassandra

分類Dev

How to use i in data.tables to select rows of all columns based on a conditional

分類Dev

How to drop all user tables?

分類Dev

Drop all tables in a database in CockroachDB

分類Dev

Join all prefixed tables into one

分類Dev

How to search all tables in a database?

分類Dev

Converting all upper case to lower case in SQL server?

分類Dev

How to get all SQL extended event sessions on server?

Related 関連記事

  1. 1

    SQL Server: How to change collation of all columns of my select on the fly

  2. 2

    SQL Search All Tables

  3. 3

    Find all boolean columns from all tables

  4. 4

    How to search a column name in all tables in a database in SQL Server 2012?

  5. 5

    How to get the MAX() while NOT grouping all columns in SQL Server

  6. 6

    SQL Server to check all columns in a databases views for a specific string value

  7. 7

    Select all columns but change a column to string

  8. 8

    Need to find out if all columns in a SQL Server table have the same value

  9. 9

    Retaining all columns in both data tables during a join, then adding a column

  10. 10

    DB2 SQL Select All With Columns As

  11. 11

    Get all columns from an SQL Table without Computed Columns

  12. 12

    SQL Server Sum Columns From Two Tables With Condition

  13. 13

    SQL Server, selecting from 2 columns from different tables

  14. 14

    Can not apply calculation to all rows in SQL Server

  15. 15

    SQL Server all(select ...)がnull

  16. 16

    How can I display all my sql tables in a ListBox

  17. 17

    How to extract all tables (including where references) within a SQL query?

  18. 18

    SQL - find all instances where two columns are the same

  19. 19

    SQL: sum of all columns in only one query using UPDATE

  20. 20

    Remove columns from SQL output where all values are 0

  21. 21

    PL/SQL : ALL_TAB_COLUMNS.table_name%TYPE value

  22. 22

    Query on all columns cassandra

  23. 23

    How to use i in data.tables to select rows of all columns based on a conditional

  24. 24

    How to drop all user tables?

  25. 25

    Drop all tables in a database in CockroachDB

  26. 26

    Join all prefixed tables into one

  27. 27

    How to search all tables in a database?

  28. 28

    Converting all upper case to lower case in SQL server?

  29. 29

    How to get all SQL extended event sessions on server?

ホットタグ

アーカイブ