如何通过t-sql从URL读取多个xml值

时间:2014-12-12 06:43:46

标签: mysql sql-server xml sql-server-2008 tsql

How to read xml in t-sql?

我已经按照上面的解决方案,但仍然无法实现它..

这是我的XML

 <result>
  <count>2</count>
  <rows>
<row>
  <f>
    <n>id</n>
    <v>8557526</v>
  </f>
  <f>
    <n>vdb_id</n>
    <v>16239</v>
  </f>
  <f>
    <n>created</n>
    <v>2014-12-10T08:50:18</v>
  </f>
  <f>
    <n>task_id</n>
    <v>5755155</v>
  </f>
  <f>
    <n>process_id</n>
    <v />
  </f>
  <f>
    <n>update_comments</n>
    <v />
  </f>
</row>
<row>
  <f>
    <n>id</n>
    <v>8567425</v>
  </f>
  <f>
    <n>vdb_id</n>
    <v>16239</v>
  </f>
  <f>
    <n>created</n>
    <v>2014-12-11T00:23:59</v>
  </f>
  <f>
    <n>task_id</n>
    <v>5755155</v>
  </f>
  <f>
    <n>process_id</n>
    <v />
  </f>        
  <f>
    <n>update_comments</n>
    <v />
  </f>
</row>

查询:

  USE tempdb
GO

  IF OBJECT_ID('tempdb..#xml') IS NOT NULL DROP TABLE #xml
  CREATE TABLE #xml ( yourXML XML )
   GO

  DECLARE @URL VARCHAR(8000) 

  DECLARE @QS varchar(50)

  SELECT @QS = '&date='+convert(varchar(25),getdate(),126)
  SELECT @URL = 'https://app.is.com/psa/api.do?function=query&table=db_time_entry&     project_id=227666&token=c9adf' + @QS


   DECLARE @Response varchar(8000)
   DECLARE @XML xml
   DECLARE @Obj int 
   DECLARE @Result int 
   DECLARE @HTTPStatus int 
   DECLARE @ErrorMsg varchar(MAX)


   EXEC @Result = sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', @Obj OUT 

   EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'GET', @URL, false
   EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded'
   EXEC @Result = sp_OAMethod @Obj, send, NULL, ''
    EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT 

    INSERT #xml ( yourXML )
    EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml'--, @Response OUT 

     --SELECT  yourXML.value('//result[1]//count[1]//rows[1]//row[1]//f[1]//n[1]//v[1]  /id','VARCHAR(MAX)') from #xml

    DECLARE @xml_value xml

     select * from #xml
     select @xml_value = yourXML  from #xml

我试过这个

 SELECT
  Key1 = Item.value('(n)[1]', 'int'),
  Key2 = Item2.value('(n)[1]', 'int'),
   ItemValue = Item2.value('(v)[1]', 'varchar(50)')
 FROM 
   @xml_value.nodes('/result/rows') AS T(Item)
 CROSS APPLY
item.nodes('row/f') AS T2(Item2)

我得到了这样的出局

    Key2    ItemValue
     id      8557526
     vdb_id   16239
     created    2014-12-10T08:50:18
     task_id    5755155
    process_id  
    update_comments 
      id    8567425
       vdb_id   16239
      created   2014-12-11T00:23:59
      task_id   5755155
     process_id 
       update_comments  

我需要输出像

ID    vdb_id   created   task_id   process_id    update_comments

 8557526   16239  2014-12-10  5755155  null   null

请有人告诉我如何从XML中选择值并获得所需的输出 提前致谢

此致

T.Navin

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

对于SQL Server,请使用PIVOT来获得所需的结果。

DECLARE @xmlData AS XML


  SET @xmlData = CAST('<result>
  <count>2</count>
  <rows>
<row>
  <f>
    <n>id</n>
    <v>8557526</v>
  </f>
  <f>
    <n>vdb_id</n>
    <v>16239</v>
  </f>
  <f>
    <n>created</n>
    <v>2014-12-10T08:50:18</v>
  </f>
  <f>
    <n>task_id</n>
    <v>5755155</v>
  </f>
  <f>
    <n>process_id</n>
    <v />
  </f>
  <f>
    <n>update_comments</n>
    <v />
  </f>
</row>
<row>
  <f>
    <n>id</n>
    <v>8567425</v>
  </f>
  <f>
    <n>vdb_id</n>
    <v>16239</v>
  </f>
  <f>
    <n>created</n>
    <v>2014-12-11T00:23:59</v>
  </f>
  <f>
    <n>task_id</n>
    <v>5755155</v>
  </f>
  <f>
    <n>process_id</n>
    <v />
  </f>        
  <f>
    <n>update_comments</n>
    <v />
  </f>
</row>
</rows>
</result>' AS XML)

SELECT Piv.Id, piv.[vdb_id], piv.[created], piv.[task_id], piv.[update_comments] 
FROM 
(
    SELECT Result1.value('v[1]','VARCHAR(200)') AS A, 
           Result1.value('n[1]','VARCHAR(200)') AS B,
         DENSE_RANK() over(order by Result) AS Num
    FROM @xmlData.nodes('//result/rows/row') xmlData(Result) 
    CROSS APPLY xmlData.Result.nodes('./f') xmlData1(Result1)
) AS A    
Pivot (Min(A) FOR B IN ([id],
                        [vdb_id],
                        [created],
                        [task_id],
                        [update_comments])
       ) piv