聲明:本人對一些版權並不深入了解,但是這個腳本是真的好,幫我解決了問題,特此收藏,並且注明了原文鏈接,如有侵權,請告知刪除。
/*
腳本作用:原來定義為decimal(18,2)類型的所有統一修改為decimal(19,4)。
作者:newsxy
來源:CSDN
原文:https://blog.csdn.net/newsxy/article/details/51280414
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
*/
-- 關閉約束
declare tb cursor for
SELECT sql='alter table ['+d.name+'] NOCHECK CONSTRAINT all'
FROM syscolumns a left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U'and d.name<>'dtproperties'
where b.name in('decimal') GROUP BY d.name
declare @sql1 varchar(1000)
open tb
fetch next from tb into @sql1
while @@fetch_status = 0
begin
print @sql1
exec(@SQL1)
fetch next from tb into @sql1
end
close tb
deallocate tb
-- 修改字段數據類型
declare tb cursor for
SELECT sql='alter table ['+d.name+'] alter column ['+a.name+'] decimal(19,4)'
FROM syscolumns a left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U'and d.name<>'dtproperties'
where b.name in('decimal') order by d.name,a.name
declare @sql2 varchar(1000)
open tb
fetch next from tb into @sql2
while @@fetch_status = 0
begin
print @sql2
exec(@SQL2)
fetch next from tb into @sql2
end
close tb
deallocate tb
-- 恢復約束
declare tb cursor for
SELECT sql='alter table ['+d.name+'] CHECK CONSTRAINT ALL'
FROM syscolumns a left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U'and d.name<>'dtproperties'
where b.name in('decimal') GROUP BY d.name
declare @sql3 varchar(1000)
open tb
fetch next from tb into @sql3
while @@fetch_status = 0
begin
print @sql3
exec(@SQL3)
fetch next from tb into @sql3
end
close tb
deallocate tb
---------------------