Click here to Skip to main content
Click here to Skip to main content

Difference between CROSS APPLY and OUTER APPLY of SQL XML

By , 19 Jul 2012
 

Introduction

The APPLY operator comes in two variants, CROSS APPLY and OUTER APPLY. It is useful for joining two SQL tables or XML expressions. CROSS APPLY is equivalent to an INNER JOIN expression and OUTER APPLY is equivalent to a LEFT OUTER JOIN expression.

Solution

Sample XML

DECLARE @XML XML = 
<span class="str">'<Root>
    <UserInfo Id="1" Name="Name 1">
        <CityInfo Id="2" City="City 1" />
        <CityInfo Id="3" City="City 2" >
            <PrefInfo Id="33" Name="Show Whether" />
        </CityInfo>
    </UserInfo>
    <UserInfo Id="2" Name="Name 2">
        <CityInfo Id="4" City="City 3" />
        <CityInfo Id="5" City="City 4" >
            <PrefInfo Id="33" Name="Show Temprature" />
        </CityInfo>
    </UserInfo>
</Root>'</span>

Cross Apply

Cross apply basically uses inner join of two XML collections

SELECT
    UserInfo.value('@Id', 'BIGINT') as UserId
    , UserInfo.value('@Name', 'VARCHAR(20)') as UserName
    , CityInfo.value('@Id', 'BIGINT') as CityId
    , CityInfo.value('@City', 'VARCHAR(20)') as CityName
    , PrefInfo.value('@Id', 'BIGINT') as PrefId
    , PrefInfo.value('@Name', 'VARCHAR(20)') as PrefName
FROM @xml.nodes('/Root/UserInfo')e(UserInfo)
    CROSS APPLY UserInfo.nodes('CityInfo')b(CityInfo)
    CROSS APPLY CityInfo.nodes('PrefInfo')c(PrefInfo)

Output

UserId   UserName  CityId  CityName  PrefId   PrefName
-------- --------- ------- --------- -------- ----------------
1        Name 1    3       City 2    33       Show Whether
2        Name 2    5       City 4    33       Show Temprature 

Outer Apply

Outer apply basically uses left outer join of two XML collections

SELECT
    UserInfo.value('@Id', 'BIGINT') as UserId
    , UserInfo.value('@Name', 'VARCHAR(20)') as UserName
    , CityInfo.value('@Id', 'BIGINT') as CityId
    , CityInfo.value('@City', 'VARCHAR(20)') as CityName
    , PrefInfo.value('@Id', 'BIGINT') as PrefId
    , PrefInfo.value('@Name', 'VARCHAR(20)') as PrefName
FROM @xml.nodes('/Root/UserInfo')e(UserInfo)
    CROSS APPLY UserInfo.nodes('CityInfo')b(CityInfo)
    OUTER APPLY CityInfo.nodes('PrefInfo')c(PrefInfo)

Output

UserId  UserName  CityId  CityName  PrefId  PrefName
------- --------- ------- --------- ------- ----------------
1       Name 1    2       City 1    NULL    NULL
1       Name 1    3       City 2    33      Show Whether
2       Name 2    4       City 3    NULL    NULL
2       Name 2    5       City 4    33      Show Temprature 

Originally posted @ JSD Ideas[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jsd24
Technical Lead
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNot Enough Content [modified]memberTim Corey16 Jul '12 - 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 19 Jul 2012
Article Copyright 2012 by jsd24
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid