柬埔寨移民:从SQL数据库test的几十个表中找出同一字段a

来源:百度文库 编辑:神马品牌网 时间:2024/04/27 15:58:31
这几十个表中,如果是同一字段名,那么则数据相同
求:这几十个表中有哪些有字段a,如果要修改这个字段的数据要怎么办?
要修改这个字段的数据(在第二位中添加一个零),是一个一个表改,还是有其他方法?

找出包含字段a的语句如下

select b.name
from test.dbo.syscolumns a,test.dbo.sysobjects b
where a.name='a' and a.id=b.id and b.type='u'

修改数据
可以用游标+动态语句

declare @sqlstr varchar(3000)
declare @tablename varchar(128)

declare cur_update cursor for
select b.name
from test.dbo.syscolumns a,test.dbo.sysobjects b
where a.name='a' and a.id=b.id and b.type='u'

open cur_update

fetch next from cur_update
into @tablename

while @@fetch_status=0
begin
set @sqlstr='update '+@tablename+' set a=stuff(a,2,0,''0'')'
exec (@sqlstr)
print @sqlstr --为了防止语句有误最好先把exec (@sqlstr)注释掉,直接把语句打印出来

fetch next from cur_update
into @tablename
end

close cur_update
deallocate cur_update