XQuery and Node Ids

前端 未结 2 1812
执念已碎
执念已碎 2021-01-25 16:07

I have this variable:

declare @xmlDoc XML

it has the following xml stored in it:



        
相关标签:
2条回答
  • 2021-01-25 16:56

    You can use a number table and position()

     SELECT  N.Number as ID,
             -- Param 1
             TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
             TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,             
             -- Param2
             TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
             TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value, 
    
             -- Param3           
             TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
             TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,
    
             -- Param 4
             TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
             TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,
    
             -- Param 5
             TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
             TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value
    
     FROM master..spt_values as N
        cross apply @xmldoc.nodes('/NewDataSet/Table1[position()=sql:column("N.Number")]') AS TBL(SParam)
     where N.type = 'P' and
           N.number between 1 and @xmlDoc.value('count(/NewDataSet/Table1)', 'int')
    
    0 讨论(0)
  • 2021-01-25 16:57

    Take a look at this connect request to Fully support position() in xquery The requestor offers a couple of work arounds that might be usful to you

    Based on the second workaround I wrote the following

    SELECT  
             p.number,
    
             TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
             TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,             
             -- Param2
             TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
             TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value, 
    
             -- Param3           
             TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
             TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,
    
             -- Param 4
             TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
             TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,
    
             -- Param 5
             TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
             TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value
     FROM    
        master..spt_values p CROSS APPLY 
    
    
         @xmldoc.nodes('NewDataSet/Table1[position()=sql:column("p.number")]') AS TBL(SParam)  
     WHERE P.type = 'P'
    

    Which returns the following

    number      Param1Name                                         Param1Value                                                                                          Param2Name                                         Param2Value                                                                                          Param3Name                                         Param3Value                                                                                          Param4Name                                         Param4Value                                                                                          Param5Name                                         Param5Value
    ----------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ----------------------------------------------------------------------------------------------------
    1           Sharedparam                                        shared                                                                                               Antoher                                            sahre                                                                                                RandomParam2                                       Good stuff                                                                                           MoreParam                                          and more                                                                                             ResultsParam                                       2
    2           RandomParam2                                       do you                                                                                               MoreParam                                          think                                                                                                ResultsParam                                       2                                                                                                                                                       NULL                                                                                                                                                    NULL
    3           Sharedparam                                        Last                                                                                                 Antoher                                             Set                                                                                                 RandomParam2                                        of                                                                                                  MoreParam                                          values                                                                                               ResultsParam                                       are here
    4                                                              NULL                                                                                                                                                    NULL                                                                                                                                                    NULL                                                                                                                                                    NULL                                                                                                                                                    NULL
    
    (4 row(s) affected)
    
    0 讨论(0)
提交回复
热议问题