(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												--刪除遊標

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