SQL 2016中的URL以返回XML

时间:2018-10-26 12:52:20

标签: sql-server xml xml-parsing

我有我的sql返回信息并建立一个url。

SELECT Address,Address2,'https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial&origins='+Address+'&destinations='+Address2+'&key=xxx' URL

FROM table

如果我使用上面创建的URL并将其复制到浏览器中,则所需的XML将出现在屏幕中。但是如何在SQL中包含XML,以便仅解析特定数据,例如旅行时间?

所以现在我有以下代码为我提供了时间:

DECLARE @Address0 varchar(100) =null
DECLARE @Address1 varchar(100)=null

SET @Address0 = 'xxx'
SET @Address1 = 'yyy'

Declare @Object as Int;
Declare @ResponseText as Varchar(8000);
Declare @serviceUrl as varchar(500)
  set @serviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial&origins=' +@Address0+
   '&destinations=' +@Address1 +'&key=keyfromgoogle'
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
                @serviceUrl, --Your Web Service Url (invoked)
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

Declare @Response as XML

SET @Response = CAST(@ResponseText AS XML);
   Declare @Time as varchar(20)
   Begin
      set @Time=@Response.value('(DistanceMatrixResponse/row/element/duration/text)[1]', 'varchar(20)') 
   End
  select 
  @Time as Time

所以我认为我要创建一个临时表来保存我的地址,然后创建一个游标以通过它并将时间添加到临时表的每一行?

0 个答案:

没有答案