查找数据库中所有表中的字段包含特定值,之后修改特定值
时间:2021-07-01 10:21:17
帮助过:23人阅读
@cloumns varchar(
40)
declare @tablename varchar(
40)
declare @str varchar(
40)
declare @counts int
declare @sql nvarchar(
2000)
declare @str2 nvarchar(
2000)
declare MyCursor
Cursor For
Select a.name
as Columns, b.name
as TableName
from syscolumns a,sysobjects b,systypes c
where a.id
= b.id
and b.type
= ‘U‘
and a.xtype
=c.xtype
and c.name
like ‘%varchar%‘
---数据库中的老值
set @str=‘a392ab5a-9972-46e2-8a32-4afe78ca1076‘
---需要更新的新值
set @str2 = ‘a392ab5a-9972-46e2-8a32-4afe78ca1076-00‘
Open MyCursor
Fetch next From MyCursor
Into @cloumns,
@tablename
While(
@@Fetch_Status = 0)
Begin
set @sql=‘select @tmp_counts=count(*) from ‘ +@tablename+ ‘ where ‘ +@cloumns+‘ = ‘‘‘ +@str+ ‘‘‘‘
execute sp_executesql
@sql,N
‘@tmp_counts int out‘,
@counts out
if @counts>0
begin
---print ‘表名为:‘+@tablename+‘,字段名为‘+@cloumns
set @sql = ‘update ‘ + @tablename + ‘ set ‘ + @cloumns + ‘=‘‘‘ + @str2 + ‘‘‘ where ‘ +@cloumns+‘ = ‘‘‘ +@str+ ‘‘‘‘
execute sp_executesql
@sql
---print @sql
end
Fetch next From MyCursor
Into @cloumns,
@tablename
End
Close MyCursor
Deallocate MyCursor
本实例是为了修改数据库中有很多个表都包含了特定的字段,现在特定字段的值需要改变。以实现批量更新。
查找数据库中所有表中的字段包含特定值,之后修改特定值
标签:blog dea arc 实现 stat pes 批量 etc cut