SQL从数据类型TEXT中连接2个大的TEXT行

时间:2014-10-06 19:03:47

标签: sql sql-server-2012

我正在努力实现以下目标:

在我的cte中,我有2个(或更多)大文本字符串。我想将这些连接成一行而不是多行。

下面是简化版。然而,对于for xml,它会搞砸......

任何想法或方法?

WITH cte AS
(
SELECT CAST('<XML></XML>' as text) as Result
UNION ALL
SELECT CAST('<XML AGAIN></XML AGAIN' as text) as Result
)


SELECT Result 
FROM cte p2
FOR XML PATH('') 

1 个答案:

答案 0 :(得分:1)

您可能会看到括号(<>)等特殊字符编码为HTML实体。

添加TYPE指令以正确编码/解码:

WITH cte1 AS
(
SELECT CAST('<XML></XML>' as text) as Result
UNION ALL
SELECT CAST('<XML AGAIN></XML AGAIN>' as text) as Result
), cte2 (XMLResult) AS
(
SELECT Result
FROM cte1
FOR XML PATH(''), TYPE
)

SELECT XMLResult.value('.','nvarchar(max)')
FROM cte2