Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

XQuery und Knoten-IDs

Sie können eine Zahlentabelle und position() verwenden

 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')