Technical Article

Trigger to replace strings in the Text field.

,

I was looking over internet trying to find a SP or UDF
to replace string accourences in the Text fields, I didn't
find any so I created ths trigger, may be it will be usefull as example. Be aware on the big documents it can be time consuming.
All it does is replacing < and > substrings of the encoded exml document when it is inserted to the database to the  characters.

If somebody have better solution for this please let me know.

--**********************************************************
--This trigger will replace < and > substrings
--in the field [Data] of [myTable] table with '<' and '>'
--characters.
--It assumes that myTable has pimary key on the [ID] field
--Please replace this with corresponding names from your 
--table.
--**********************************************************

CREATE TRIGGER decodeXML ON [dbo].[myTable] 
AFTER INSERT
AS

DECLARE @ptrData binary(16)
DECLARE @pos Integer
DECLARE @id Integer

select @id=id from inserted

SELECT @ptrData = TEXTPTR(data) 
   FROM [myTable] where id = @id

--Replace < string with <
select @pos=PATINDEX('%<%', data)-1
     FROM [myTable] where id = @id

WHILE @pos >=0
BEGIN
UPDATETEXT [myTable].[data] @ptrData @pos 4 '<' 
select @pos=PATINDEX('%<%', data)-1
      FROM [myTable] where id = @id
END

--Replace > string with >
select @pos=PATINDEX('%>%', data)-1
     FROM [myTable] where id = @id

WHILE @pos >=0
BEGIN
UPDATETEXT [myTable].[data] @ptrData @pos 4 '>' 
select @pos=PATINDEX('%>%', data)-1
      FROM [myTable] where id = @id
END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating