標籤:style blog http color os ar 使用 for sp
1、使用遊標
declare @sql varchar(200), @name varchar(40)declare my_cursor scroll cursor for select name from sysobjects where type = ‘u‘ and name like ‘jobs_%‘open my_cursorfetch next from my_cursor into @namewhile(@@fetch_status=0)begin print ‘processing ‘ + @name set @sql = N‘alter table testbfcmisuser.‘ + @name + N‘ add note varchar(200)‘ exec(@sql) print ‘finished‘ fetch next from my_cursor into @nameendclose my_cursordeallocate my_cursor
2、使用暫存資料表
declare @sql varchar(200), @name varchar(100), @count intselect identity(int, 1, 1) as ID, name into #JobTable from sysobjects where type = ‘u‘ and name like ‘Jobs_%‘set @count=@@rowcountwhile @count>0begin select top 1 @name = name from #JobTable where id = @count print ‘processing ‘ + @name set @sql = N‘alter table testbfcmisuser.‘ + @name + N‘ add note varchar(200)‘ exec(@sql) print ‘finished‘ set @count = @count - 1enddrop table #JobTable
http://www.wonima.com 喔尼瑪網,搞笑網站
SQLServer在多個表中都增加一個欄位的方法