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条回答 默认 最新

  • Y_L_Y_L 2017-08-14 08:36
    关注

    自己结一下吧

     declare @cloumns varchar(40)
    declare @tablename varchar(40)
    declare @str varchar(40)
    declare @counts int
    declare @sql nvarchar(2000)
    declare @updateSql 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 '%char%'
    set @str='eb130b34-1516-43cb-808f-d26a90ea6cf3'  
    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 '***************************************************'
     print '表名为:'+@tablename+',字段名为'+@cloumns
     select @updateSql = 'UPDATE '+@tableName+ ' SET '+ @cloumns+' = ''421dda10-7dbe-4d19-89e4-1e782bc4ee48'' WHERE '+ @cloumns+ ' = '''+ @str +''''
     exec(@updateSql) 
     print '表名为:'+@tablename+',字段名为'+@cloumns+'已更改'
    
    
    end
    Fetch next From MyCursor Into @cloumns,@tablename
    End
    Close MyCursor
    Deallocate MyCursor
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?