Using UNION ALL in STUFF / XML Path

后端 未结 1 1245
梦如初夏
梦如初夏 2021-01-04 10:39

Msg 1086, Level 15, State 1, Line 20 The FOR XML clause is invalid in views, inline functions, derived tables, and subqueries when they contain a set operator. To work aroun

相关标签:
1条回答
  • 2021-01-04 11:26

    There's a simple workaround for that, you should wrap your union query(or any derived table for that matter) with another select. Do this and then continue the syntax normally:

    select * from
    (
    SELECT 1 as I
    UNION ALL
    SELECT 2 as J
    ) as K
    

    Something like this is what you're searching for:

    SELECT  STUFF((
        select * from(
    
        SELECT * from dbo.Table1 as I
        UNION ALL
        SELECT * from dbo.Table2 as j
        ) as k
        FOR XML PATH('')
        ),1,0,'')
    

    I checked and it works flawlessly

    0 讨论(0)
提交回复
热议问题