问题
Right now I am running a query to find and replace a dynamic word within a document into a link. It works just fine but returns null
when there are some special characters. Currently I am having issues with the dynamic word contains an ampersand '&'
Here is part of the content:
<p>STEPS:</p>
<p>
<p>Please refer to <|Cease & Desist|> policy.</p>
It is returning a NULL
for 'Cease & Desist'
Below is the code I am using, what I need is for it to return the entire dynamic word even if it contains the ampersand.
SET NOCOUNT ON;
DECLARE @MyTable TABLE
(
ID INT IDENTITY(1,1) PRIMARY KEY,
PolicyName NVARCHAR(200) NULL,
DocumentID int not null,
OldContent NVARCHAR(MAX) NOT NULL,
NewContent NVARCHAR(MAX) NULL
);
INSERT INTO @MyTable (documentid,OldContent)
select documentid, html from IPACS_Document where PATINDEX('%Please refer to <|%', html) > 0 and PATINDEX('%|> Policy%', html) > 0 and documentid = 147;
--select html, PATINDEX('%Please refer to <|%', html), PATINDEX('%|> Policy%', html) from IPACS_Document where PATINDEX('%Please refer to <|%', html) > 0;
WITH UpdateCTE
AS
(
SELECT b.PolicyName, b.[text], b.NewContent,STUFF(b.InnerText,b.StartIndex-5,b.EndIndex-b.StartIndex+10,'<a href="~/Document/Details/'+ CAST(d.[documentid] as VARCHAR(200))+'">'+b.[Text]+'</a>') AS ChangedText
FROM
(
SELECT a.*,SUBSTRING(a.InnerText,a.StartIndex,a.EndIndex-a.StartIndex) AS [Text]
FROM
(
SELECT PATINDEX('%Please refer to <|%',t.OldContent)+21 AS StartIndex,
PATINDEX('%|> Policy%',t.OldContent) AS EndIndex,
t.OldContent AS InnerText,
t.NewContent,
t.PolicyName
FROM @MyTable t
) a
) b
inner join IPACS_Document d on d.filename like '%' + b.[text] + '%'
where d.categoryid = 3
)
--select * from UpdateCTE
UPDATE UpdateCTE
SET NewContent = ChangedText, PolicyName = [text];
SELECT *
FROM @MyTable x;
In the above query the most inner query query 'a'
InnerText column name [text]
is what contains the dynamic word. This is passed to table 'b'
as [text]
. Eventually in @Mytable
the dynamic word is put into PolicyName
.
How can I allow this to work when the dynamic word contains an ampersand or special character?
回答1:
SET NOCOUNT ON;
DECLARE @MyTable TABLE
(
ID INT IDENTITY(1,1) PRIMARY KEY,
PolicyName NVARCHAR(200) NULL,
DocumentID int not null,
OldContent NVARCHAR(MAX) NOT NULL,
NewContent NVARCHAR(MAX) NULL
);
INSERT INTO @MyTable (PolicyName,OldContent,DocumentID)
VALUES (NULL,N'<p>STEPS:</p>
<p>
<p>Please refer to <|Cease & Desist|> policy.</p>',123);
;WITH UpdateCTE
AS
(
SELECT b.PolicyName, b.[text], b.NewContent,STUFF(b.InnerText,b.StartIndex-5,b.EndIndex-b.StartIndex+10,'<a href="~/Document/Details/'+ CAST(b.[documentid] as VARCHAR(200))+'">'+b.[Text]+'</a>') AS ChangedText
FROM
(
SELECT a.*,SUBSTRING(a.InnerText,a.StartIndex,a.EndIndex-a.StartIndex) AS [Text]
FROM
(
SELECT PATINDEX('%Please refer to <|%',t.OldContent)+21 AS StartIndex,
PATINDEX('%|> Policy%',t.OldContent) AS EndIndex,
t.OldContent AS InnerText,
t.NewContent,
t.PolicyName,
t.DocumentID
FROM @MyTable t
) a
) b
--inner join IPACS_Document d on d.filename like '%' + b.[text] + '%'
--where d.categoryid = 3
)
--select * from UpdateCTE
UPDATE UpdateCTE
SET NewContent = ChangedText, PolicyName = [text];
SELECT * FROM @MyTable x;
Results:
ID PolicyName DocumentID OldContent NewContent
-- ------------------- ----------- ----------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 Cease & Desist 123 <p>STEPS:</p> <p> <p>Please refer to <|Cease & Desist|> policy.</p> <p>STEPS:</p> <p> <p>Please refer to <a href="~/Document/Details/123">Cease & Desist</a> policy.</p>
回答2:
I would think your problem may be, not sure the conversion of what appears to be an html element into a table and using a table variable veruss a permanent or temp table (#temp). I can get substrings evaluating index and pass them to a temp table just fine, I cannot to a table variable. This is using SQL 2012 as my version.
There are potential issues with using a substring too, namely you do zero validation on proper formatted XHTML. I would personally use an xml query and/or value parsing after casting the xhtml to xml. However this would show the values as they ultimately appear and not be what you wish. However I have given your substring method as well and it will still work if you wish the data to be kept as marked up. Self extracting example below:
declare @X varchar(256) = ' <p>STEPS:</p>
<p>
<p>Please refer to <|Cease & Desist|> policy.</p>
<p>Just extra stuff ;lkasdlkjasdlfkjasdf;lasdjfl;asdjf;lsadjkf;dsaljfas;dljfsdalfjsadlkfjasd;lfk</p>
</p>'
if object_id('tempdb..#Temp') is not null
drop table #temp
;
With a as
(
Select
@X as AsIS
, cast(@X as varchar(128)) as Truncated
, PATINDEX('%Please refer to <|%', @X)+21 AS St
, PATINDEX('%|> Policy%', @X) as Ed
--, cast( cast(@X as varchar(128)) as xml) -- WILL BREAK ELEMENTS
, cast(@X as xml) as AsXml
)
Select
Substring(AsIs, St, Ed - St) as SubStringMethod
, AsXml.query('p/p').value('p[1]/.', 'varchar(256)') as xmlqueryvalue
into #temp
from a
declare @temp table ( submethod varchar(256), xmlmethod xml)
;
--insert into @temp -- WILL BLOW UP with
--XML parsing: line 1, character 18, illegal qualified name character
Select *
from #temp
来源:https://stackoverflow.com/questions/18317635/dynamic-sql-query-and-problems-with-special-characters