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

求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
    
    打赏 评论
  • Tsui丶 2017-08-07 03:48

    图片说明 图片位置报错

    'like'+'%'+@str+'%' 自己检查下

    打赏 评论
  • wlq199101 2017-08-07 04:01
      select query = 'if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'') 
        --....
    

    改成

       select query = 'if exists(select 1 from ['+b.name+'] where ['+a.name+'] like  ''' + '%'+ @str+ '%' +''') 
         --...
         --' 默认情况下, '是字符串的边界符, 如果在字符串中包含', 则必须使用两个', 第1个'就是转义符 
    
    打赏 评论
  • 0与1之间 2017-08-07 06:22

    应该是更新like这少了一个空格吧
    这样试试:
    select @updateSql = 'UPDATE '+@tableNmae+ ' SET '+ @columnName+' = LOWER(NEWID()) WHERE '+ @columnName+ ' like ''%'+ @str +'%'''

    打赏 评论
  • wodewojueding 2017-08-07 07:19

    更新语句,like前面与字段之间少了空格

    打赏 评论

相关推荐 更多相似问题