Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I reset collation for all columns in the database?

I need to reset collation for all columns in all tables in the database: enter image description here

I want to use default collation of database

I tried to change it under database properties: enter image description here

but collation already setted in columns and it mean that i cannot overwrite it

anybody has script that can do it for me?

like image 246
Sasha Avatar asked Jun 29 '11 06:06

Sasha


People also ask

How do you find a column collation?

To view a collation setting for a column in Object Explorer Expand Databases, expand the database and then expand Tables. Expand the table that contains the column and then expand Columns. Right-click the column and select Properties. If the collation property is empty, the column is not a character data type.

How do I change the column collation in mysql workbench?

Goto PhpMyAdmin->Operations->Collation. There you an find the select box which contains all the exsiting collations. So that here you can change your collation. So here after database table will follows this collation while you are creating new column .


2 Answers

I've knocked together a script that should do a decent enough job (hopefully). Run the script in the appropriate database, with results as text. Then Copy & Paste the output into a script window to change the collation of each column:

declare @NewCollationName sysname
set @NewCollationName = 'Latin1_General_CS_AS'
select
    'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(st.schema_id)) + '.' + QUOTENAME(st.name) +
    ' ALTER COLUMN ' + QUOTENAME(sc.name) + ' ' + styp.name + '(' +
    CASE WHEN sc.max_length = -1 THEN 'max' ELSE CONVERT(varchar(10),sc.max_length) END +
    ') collate ' + @NewCollationName + '
    go
    '
from
    sys.columns sc
        inner join
    sys.tables st
        on
            sc.object_id = st.object_id
        inner join
    sys.types styp
        on
            sc.user_type_id = styp.user_type_id
where
    sc.collation_name is not null and
    OBJECTPROPERTY(st.object_id,N'IsMSShipped')=0

One thing to note, however, is that the generated script won't work if the columns are the target of constraints or targetted by a schema bound object (view or function).

In such cases, you'd have to script out the dependent objects, drop them from the database, then run the script generated by the script above, and finally re-add the dependent objects.

like image 74
Damien_The_Unbeliever Avatar answered Nov 14 '22 22:11

Damien_The_Unbeliever


See (Changing the Database Collation) http://msdn.microsoft.com/en-us/library/ms174269.aspx

ALTER DATABASE database_name COLLATE collation_name
like image 23
Dan D. Avatar answered Nov 14 '22 22:11

Dan D.