(2期) 数据库所有表批量增加字段

/*********************************************************************************************
Function:数据库所有表批量增加字段
Author:Bean
Date:2012-09-24
*********************************************************************************************/
--给系统所有主表增加 VMDTIME(修改时间) 和 VMDUSER(修改人) 两个字段
--前提:所有主表采用_1结尾(Landa V8机制)
declare @tbname varchar(100)									--定义表明 接收游标的值
declare mycursor cursor for										--定义游标
	--查询系统所有主表 
	select Name from sysobjects a						
	where xtype='u'										
		and right(name,2)='_1'
	and not exists 
	(
		select 1 from syscolumns b
		where b.id=object_id(a.name)
		and (b.name='VMDTIME' or b.name='VMDUSER' )
	)
open mycursor													--打开游标
fetch next from mycursor into @tbname							--获取数据
while @@fetch_status=0											--循环
Begin												
	exec('alter table '+@tbname+' add VMDTIME datetime')		--增加VMDTIME字段
	exec('alter table '+@tbname+' add VMDUSER varchar(100)')	--增加VMDUSER字段
	fetch next from mycursor into @tbname						--循环获取数据
End
close mycursor													--关闭游标
deallocate mycursor												--删除游标

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章