Technical articles - decimal field type batch processing from two to four decimal sql, database storage solution accuracy

Disclaimer: I do not understand some of the copyright, but the script is really good, helped me solve the problem, hereby collections, and indicate the original link, if infringement, please inform deleted.

/ *
Script action: The original is defined as decimal (18,2) type of unity of all modifications to decimal (19,4).
Author: newsxy
Source: CSDN
Original: https: //blog.csdn.net/newsxy/article/details/51280414
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!
* /
- closure restraint
DECLARE Cursor TB for
the SELECT SQL = 'ALTER Table [' + + d.name '] All the NOCHECK CONSTRAINT'
the FROM A left the syscolumns the Join the systypes B = ON a.xtype b.xusertype
Inner the Join the sysobjects D A ON. = = d.id and d.xtype ID 'U'and d.name <>' the dtproperties'
WHERE b.name in ( 'decimal') the GROUP BY D.








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
---------------------

Guess you like

Origin www.cnblogs.com/bkygkx/p/11423019.html