这段SQL就是要查出某一个ID值在数据库全局的使用情况,然后用新的ID替换掉原来的数据。
但是会一直报错,如图,求解!如果有更好的写法更好!
declare @str varchar(100)
set @str='3a28c205-df29-40b0-b246-74fd8aa70a12';
declare @tablea table(name sysname, status tinyint, xusertype smallint, id int)
insert into @tablea
select name,status,xusertype,id from syscolumns;
declare @tableb table(name sysname, xtype char(2), id int)
insert into @tableb
select name,xtype,id from sysobjects;
declare @tableNmae varchar(100);
declare @columnName varchar(100);
declare @query varchar(8000);
declare @updateSql varchar(8000)
declare curs cursor local for
select query = 'if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'') print ''所在的表及字段: ['+b.name+'].['+a.name+']'''
from @tablea a join @tableb b on a.id=b.id
where b.xtype='U' and a.status>=0 and a.xusertype in(175,239,231,167) ;
select @tableNmae = name from @tableb ;
select @columnName = name from @tablea;
select @updateSql = 'UPDATE '+@tableNmae+ ' SET '+ @columnName+' = LOWER(NEWID()) WHERE '+ @columnName+ 'like ''%'+ @str +'%'''
open curs
fetch next from curs into @query
while @@fetch_status=0 begin
exec(@query)
exec(@updateSql)
fetch next from curs into @query
end
close curs
deallocate curs
图片: