I have a stored procedure that creates an XML string based on some selects.
Is there a way I can show a default if one of the selects returns NULL ? Basically I just want to keep the same structure in my XML independent of whether a value is NULL or not (currently the XML only shows records with a value).
Example stored procedure:
SELECT A.ranking,
(
SELECT B.groupName,
(
SELECT C.policy,
C.groupCount
FROM @temp C
WHERE C.ranking = A.ranking
AND C.groupName = B.groupName
ORDER BY C.groupCount desc, C.policy
FOR XML PATH(''), ELEMENTS, TYPE
) AS groupName
FROM @temp B
WHERE B.ranking = A.ranking
GROUP BY B.groupName
ORDER BY B.groupName
FOR XML PATH(''), ELEMENTS, TYPE
)
FROM @temp A
GROUP BY A.ranking
ORDER BY A.ranking
FOR XML PATH('policyRanking'), ELEMENTS, TYPE, ROOT('ranks')
Many thanks for any help with this, Tim.
使用ISNULL函数,方法是将每个NULLable列(请参见表定义)替换为以下表达式:
ISNULL ( column , default_value )
Try something like this
SELECT coalesce(A.ranking,0) as ranking
(
SELECT coalesce(B.groupName,'') as groupName,
(
SELECT C.policy,
C.groupCount
FROM @temp C
WHERE C.ranking = A.ranking
AND C.groupName = B.groupName
ORDER BY C.groupCount desc, C.policy
FOR XML PATH(''), ELEMENTS, TYPE
) AS groupName
FROM @temp B
WHERE B.ranking = A.ranking
GROUP BY B.groupName
ORDER BY B.groupName
FOR XML PATH(''), ELEMENTS, TYPE
)
FROM @temp A
GROUP BY A.ranking
ORDER BY A.ranking
FOR XML PATH('policyRanking'), ELEMENTS, TYPE, ROOT('ranks')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.