0

現在、ドキュメント内の動的な単語を検索してリンクに置き換えるクエリを実行しています。null問題なく動作しますが、特殊文字があると戻ります。現在、動的な単語に問題があり、ampersand '&'

内容の一部は次のとおりです。

 <p>STEPS:</p>
    <p>
    <p>Please refer to &lt;|Cease &amp; Desist|&gt; policy.</p>

NULLfor を返しています'Cease &amp; Desist'

以下は私が使用しているコードです。必要なのは、アンパサンドが含まれていても動的単語全体を返すことです。

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;

上記のクエリでは、最も内側のクエリquery 'a' InnerText column name [text]に動的単語が含まれています。table 'b'これはasに渡され[text]ます。最終的に@Mytable動的な単語は に入れられPolicyNameます。

動的単語にアンパサンドまたは特殊文字が含まれている場合、これを機能させるにはどうすればよいですか?

4

2 に答える 2

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;

結果:

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>
于 2013-08-19T16:24:57.803 に答える