热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> 数据库>>正文内容

删除MSSQL数据库text字段中恶意脚本<Script="***.js"></script>的方法(downmoon)

发布时间:2010年06月17日点击数: 佚名

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

  1. -select * from Product where P_Intro like '%<script src=http://my.stsw518.cn/a002/1.js></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='<script src=http://my.stsw518.cn/a002/1.js></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 '%<script src=http://my.stsw518.cn/a002/1.js></script>%'  
  17. -- where  P_ID=300727   ----where P_Intro like '%<script src=http://my.stsw518.cn/a002/1.js></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   

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作