gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:47:07 24 4
gpt4 key购买 nike

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.纳文

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

最佳答案

对于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

关于mysql - 我如何通过 t-sql 从 URL 读取多个 xml 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com