When should I use cross apply over inner join?

前端 未结 14 1053
忘了有多久
忘了有多久 2020-11-22 06:51

What is the main purpose of using CROSS APPLY?

I have read (vaguely, through posts on the Internet) that cross apply can be more efficient when selectin

14条回答
  •  北海茫月
    2020-11-22 07:15

    Cross apply works well with an XML field as well. If you wish to select node values in combination with other fields.

    For example, if you have a table containing some xml

    
        
           
           
           
           
        
    
    

    Using the query

    SELECT
           id as [xt_id]
          ,xmlfield.value('(/root/@attribute)[1]', 'varchar(50)') root_attribute_value
      ,node_attribute_value = [some_node].value('@value', 'int')
      ,lt.lt_name   
    FROM dbo.table_with_xml xt
    CROSS APPLY xmlfield.nodes('/root/subnode1/some_node') as g ([some_node])
    LEFT OUTER JOIN dbo.lookup_table lt
    ON [some_node].value('@value', 'int') = lt.lt_id
    

    Will return a result

    xt_id root_attribute_value node_attribute_value lt_name
    ----------------------------------------------------------------------
    1     test1            1                    Benefits
    1     test1            4                    FINRPTCOMPANY
    

提交回复
热议问题