Finding node order in XML document in SQL Server

前端 未结 6 1392
死守一世寂寞
死守一世寂寞 2020-12-01 04:41

How can I find the order of nodes in an XML document?

What I have is a document like this:


    
            


        
6条回答
  •  春和景丽
    2020-12-01 05:23

    You can get the position of the xml returned by a x.nodes() function like so:

    row_number() over (order by (select 0))
    

    For example:

    DECLARE @x XML
    SET @x = 'abc1def1abc2def2'
    
    SELECT
        b.query('.'),
        row_number() over (partition by 0 order by (select 0))
    FROM
        @x.nodes('/a/b') x(b)
    

提交回复
热议问题