sql serveR语言 将 " 替换为 sql 中 varchar 变量的 "e



我正试图用&quot替换"replace(@variable,'"','"')没有帮助,在XML中我得到了

<tag>&amp quot;Test&amp quot;</tag> 

而不是

<tag>&quot;test&quot;</tag>

示例:

DECLARE @Text varchar(20) = '"Test"'
SET @Text = REPLACE(@Text,'"','&quot ;')
SELECT @Text
DECLARE @Table table (ID int)
INSERT INTO @Table
    (ID)
VALUES
    (123)
DECLARE @TestXml AS XML = ( 
    SELECT
        @Text
    FROM @Table AS tag  
    FOR XML AUTO, ELEMENTS
    )   
SELECT @TestXml

提前感谢

如果问题出现在选择查询中,请尝试下面的sql

更改

SELECT @TestXml

SELECT '<tag>' + @TestXml.value('/tag[1]','varchar(max)') + '</tag>'  as 'Word'

您将在选择查询中得到以下结果:

Word
-------------------------------
<tag>&quot;Test&quot;</tag>

更新

保留"&"char我们需要<![CDATA[…]>尝试低于sql

Declare @text nvarchar(20) = '"Test"'
select @text = replace(@text,'"','&quot;')
Declare @table table (ID int)
Declare @xml as nvarchar(Max)
insert into @table values (123)
set @xml=( Select 
    1 as tag,
    Null as Parent,
    @text as [tag!1!!CDATA]
from @table 
for xml Explicit
)
select @xml

结果:

result
---------------------------------------
<tag><![CDATA[&quot;Test&quot;]]></tag>

最新更新