这几天维护公司服务器。服务器上都安装部署了几十个sql server 数据库。虽然数据库名字不一样,但表结构都一样。现在需要对这几十个数据库中的某张表的某个字段做一个更新
假如表名为 sms ,要更改的字段为 passwd.
在网上找到了一个方法:通过游标解决,具体sql语句如下:
declare @dbname nvarchar(20)
declare curdb cursor for
select name from master..sysdatabases where dbid>4 --找出所有要更新的用户数据库(dbid<=4的为 master、msdb、model、tempdb 4个sql server系统库)
open curdb
fetch next from curdb into @dbname
while @@fetch_status=0
begin
exec ('update '+ @dbname +'..sms SET passwd=''设置新密码''' )
fetch next from curdb into @dbname
end
close curdb
deallocate curdb