改变SQL Server数据库排序规则

问题描述:

是否有可能改变整理数据库下的所有对象?改变SQL Server数据库排序规则

例如,如果我更改数据库排序规则,它应该改变所有的对象(表,特效等)与该数据库。

+0

[更改SQL Server数据库排序(http://*.com/questions/2938422/changing-sql-server-database-sorting) – gbn 2010-09-07 03:48:59

我有这个问题,前一段时间,当我们改变了底层数据库支持Unicode和需要改变旧的数据库上的字符集,以支持匈牙利。

该脚本将让你很长的路要走改变整理;您将需要手动更改主DB数据集。我不必长时间运行它,它不能修复任何计算列,但可能还有其他问题。不要在没有经过测试的情况下在实时数据库中运行它 - 您可能想要选择它所做的更改,以便您可以审核或找出稍后错过的更改。

Declare 
    @NewCollation varchar(255), @DBName sysname 
Select @NewCollation = 'Latin_1_CI_AI', -- change this to the collation that you need 
     @DBName = DB_NAME() 

Declare 
    @CName varchar(255), @TbleName sysname, @objOwner sysname, @Sql varchar(8000), @Size int, @Status tinyint, @Colorder int 

Declare CurWhileLoop cursor read_only forward_only local 
for Select 
     QUOTENAME(C.Name) 
     ,T.Name 
     ,QUOTENAME(U.Name) + '.' +QUOTENAME(O.Name) 
     ,C.Prec 
     ,C.isnullable 
     ,C.colorder 
    From syscolumns C 
     inner join systypes T on C.xtype=T.xtype 
     inner join sysobjects O on C.ID=O.ID 
     inner join sysusers u on O.uid = u.uid 
    where T.Name in ('varchar', 'char', 'text', 'nchar', 'nvarchar', 'ntext') 
     and O.xtype in ('U') 
     and C.collation != @NewCollation 
    and objectProperty(O.ID, 'ismsshipped')=0 
    order by 3, 1 

open CurWhileLoop 
SET XACT_ABORT ON 
begin tran 
fetch CurWhileLoop into @CName, @TbleName, @objOwner, @Size, @Status, @Colorder 
while @@FETCH_STATUS =0 
begin 
    set @Sql='ALTER TABLE '[email protected]+' ALTER COLUMN '[email protected]+' '[email protected]+ isnull ('(' 
+convert(varchar,@Size)+')', '') +' COLLATE '+ @NewCollation 
+' '+case when @Status=1 then 'NULL' else 'NOT NULL' end 
    exec(@Sql) -- change this to print if you need only the script, not the action 
    fetch CurWhileLoop into @CName, @TbleName, @objOwner, @Size, @Status, @Colorder 
end 
close CurWhileLoop 
deallocate CurWhileLoop 
commit tran 
+0

你好,我想你的脚本,可能的重复,但索引正在阻止我更改列归类(表示对象取决于列)。你知道这个解决方法吗?谢谢! – 2010-11-22 14:56:08

+1

你将不得不放弃他们都害怕。你可以用管理工作室把它们编出来,然后放回去。 – u07ch 2010-11-24 11:48:23