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 回答
0

我认为您的问题可能是,不确定将似乎是 html 元素的内容转换为表并使用表变量与永久表或临时表(#temp)。我可以获取评估索引的子字符串并将它们传递给临时表就好了,我不能传递给表变量。这是使用 SQL 2012 作为我的版本。

使用子字符串也有潜在的问题,即您对正确格式化的 XHTML 进行零验证。在将 xhtml 转换为 xml 之后,我会亲自使用 xml 查询和/或值解析。但是,这将显示最终出现的值,而不是您想要的。但是,我也提供了您的子字符串方法,如果您希望数据保持标记状态,它仍然可以工作。下面的自提取示例:

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
于 2013-08-19T16:11:02.280 回答