STUFF((SELECT distinct ',' + QUOTENAME(c.Error_Code)
FROM
(SELECT Connection_type, Error_Code, Count
FROM
(SELECT
Connection_Type, error_code, count(*) AS count,
row_number() over(partition by Connection_Type order by count(*) desc) as ROWNUM
FROM
Staging
WHERE
TransactionDate >= convert(varchar, getdate() -1, 111)
AND Status != 'Deliver'
GROUP BY
Connection_Type, error_code) a
WHERE
rownum <= 10) c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
“TYPE”是什么意思?使用TYPE
的意义何在?答案 0 :(得分:3)
返回一个类型为XML的值。
一个不使用它的常见替代方法,只是直接以字符串形式返回。
SELECT STUFF((SELECT ',' + QUOTENAME(c.Error_Code)
FROM (VALUES('FOO & BAR'),
('1 < 4 ') ) c(Error_Code)
FOR XML PATH('')), 1, 1, '')
但这不能正确处理XML入侵并返回
[FOO & BAR],[1 < 4 ]
返回XML数据类型并正确调用.value
返回
[FOO & BAR],[1 < 4 ]