您的位置:首页 > 数据库

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

2012-09-24 17:06 225 查看
/*********************************************************************************************
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												--删除游标
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: