我正试图用"
替换"
,replace(@variable,'"','"')
没有帮助,在XML中我得到了
<tag>& quot;Test& quot;</tag>
而不是
<tag>"test"</tag>
示例:
DECLARE @Text varchar(20) = '"Test"'
SET @Text = REPLACE(@Text,'"','" ;')
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>"Test"</tag>
更新
保留"&"char我们需要<![CDATA[…]>尝试低于sql
Declare @text nvarchar(20) = '"Test"'
select @text = replace(@text,'"','"')
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["Test"]]></tag>