2010-09-07 45 views
3

是否有可能改變整理數據庫下的所有對象?改變SQL Server數據庫排序規則

例如,如果我更改數據庫排序規則,它應該改變所有的對象(表,特效等)與該數據庫。

+0

[更改SQL Server數據庫排序(http://stackoverflow.com/questions/2938422/changing-sql-server-database-sorting) – gbn 2010-09-07 03:48:59

回答

3

我有這個問題,前一段時間,當我們改變了底層數據庫支持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