문제

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.

도움이 되었습니까?

해결책 2

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')

다른 팁

Use ISNULL function, by replacing each NULLable column (see table definition) with the expression:

 ISNULL ( column , default_value )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top