Y_L_Y_L
2017-08-07 03:42
采纳率: 87.5%
浏览 831

求SQL大神帮忙解决问题!!!!!

这段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

图片:
图片说明

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

相关推荐 更多相似问题