Dynamic SQL Query and problems with special characters

懵懂的女人 提交于 2020-01-26 02:04:39

问题


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 &lt;|Cease &amp; Desist|&gt; policy.</p>

It is returning a NULL for 'Cease &amp; 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 &lt;|%', html) > 0 and PATINDEX('%|&gt; Policy%', html) > 0 and documentid = 147;
--select html, PATINDEX('%Please refer to &lt;|%', html), PATINDEX('%|&gt; Policy%', html) from IPACS_Document where PATINDEX('%Please refer to &lt;|%', 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 &lt;|%',t.OldContent)+21 AS StartIndex,
                    PATINDEX('%|&gt; 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 &lt;|Cease &amp; Desist|&gt; 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 &lt;|%',t.OldContent)+21 AS StartIndex,
                    PATINDEX('%|&gt; 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 &amp; Desist  123         <p>STEPS:</p> <p> <p>Please refer to &lt;|Cease &amp; Desist|&gt; policy.</p> <p>STEPS:</p> <p> <p>Please refer to <a href="~/Document/Details/123">Cease &amp; 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 &lt;|Cease &amp; Desist|&gt; 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 &lt;|%', @X)+21 AS  St
    ,   PATINDEX('%|&gt; 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!