2

如何将 OldValue 包装在 CDATA 中?

SELECT
1 AS Tag,
0 AS Parent,
(
    SELECT PositionDescription AS 'data()'
     FROM dbo.Project where ID = 32
     FOR XML PATH('')
) AS 'PositionDescription!1!OldValue!CDATA'
FOR XML EXPLICIT, TYPE 
4

1 回答 1

5

只需删除 TYPE 关键字。并在子查询中使用原始字符串而不是 XML

SELECT
1 AS Tag,
0 AS Parent,
(
    SELECT PositionDescription
      FROM dbo.Project where ID = 32
) AS [PositionDescription!1!OldValue!CDATA]
FOR XML EXPLICIT

样品和输出

create table Project (PositionDescription varchar(100), id int)
insert project select 'abc<test>', 32
---

<PositionDescription>
  <OldValue><![CDATA[abc<test>]]></OldValue>
</PositionDescription>
于 2012-09-18T21:59:37.570 回答