删除MSSQL数据库text字段中恶意脚本的方法

来源:互联网 发布:c语言中取绝对值函数 编辑:程序博客网 时间:2024/05/22 00:43

删除MSSQL数据库text字段的替换处理示例--全表替换,看到有人提问,所以整理了一个好久以前的处理方法,以供大家参考:
方法很简单:text字段不能使用Replace,所以使用patindex

 

view plaincopy to clipboardprint?
  1. -select * from Product where P_Intro like '%<mce:script src="http://my.stsw518.cn/a002/1.js" mce_src="http://my.stsw518.cn/a002/1.js"></mce:script>%'   
  2. --text字段的替换处理示例--全表替换    
  3. --  select   datalength(P_Intro),*   from   Product    
  4. --邀月 整理  
  5.   --定义替换的字符串    
  6.   declare   @s_str   nvarchar(4000),@d_str   nvarchar(4000)    
  7.   select   @s_str='<mce:script src="http://my.stsw518.cn/a002/1.js" mce_src="http://my.stsw518.cn/a002/1.js"></mce:script>'   --要替换的字符串    
  8.   ,@d_str='' --替换成的字符串    
  9.      
  10.      
  11.   --因为只能用patindex,所以对于搜索字符串做处理    
  12.   set   @s_str='%'+@s_str+'%'    
  13.      
  14.   --定义游标,循环处理数据    
  15.   declare   @id   bigint  
  16.   declare   #tb   cursor   for   select   P_ID   from   Product where P_Intro like '%<mce:script src="http://my.stsw518.cn/a002/1.js" mce_src="http://my.stsw518.cn/a002/1.js"></mce:script>%'   
  17. -- where  P_ID=300727   ----where P_Intro like '%<mce:script src="http://my.stsw518.cn/a002/1.js" mce_src="http://my.stsw518.cn/a002/1.js"></mce:script>%'   
  18.   open   #tb    
  19.   fetch   next   from   #tb   into   @id    
  20.   while   @@fetch_status=0    
  21.   begin    
  22.   --字符串替换处理    
  23.   declare   @p   varbinary(16)    
  24.   ,@p1   int,@p2   int    
  25.   ,@rplen   int,@step   int,@len   int    
  26.      
  27.   select   @p=textptr(P_Intro)    
  28.   ,@rplen=len(@s_str)-2    
  29.   ,@step=len(@d_str)    
  30.   ,@p1=patindex(@s_str,P_Intro)    
  31.   ,@len=datalength(P_Intro)    
  32.   ,@p2=0    
  33.   from   Product     
  34. where   P_id=@id    
  35.      
  36.   while   @p1>0    
  37.   begin    
  38.   set   @p2=@p1+@p2-1    
  39.   updatetext   Product.P_Intro   @p   @p2   @rplen   @d_str    
  40.   select   @p2=@p2+1,@p1=patindex(@s_str,substring(P_Intro,@p2+1,@len))    
  41.   from   Product   where   P_ID=@id    
  42.   end    
  43.   fetch   next   from   #tb   into   @id    
  44.   end    
  45.   close   #tb    
  46.   deallocate   #tb    
  47.      
  48.   --显示结果    
  49. ----  select   datalength(P_Intro),*   from   Product   

 

原创粉丝点击