I've created a stored procedure that uses a temp table to take the massive results of a Bing maps XML and convert it to varchar
so that I can use it to retrieve several fields. The reason for converting it to varchar
was because the XML was too large for the sp_OAMethod
and my variable was always blank.
set @serviceUrl = 'https://dev.virtualearth.net/REST/v1/Routes/Truck?wp.0=' + @ToAddress + '&wp.1=' + @FromAddress + '&vehicleHazardousMaterials=Flammable&output=xml&key=XXX-000-XXX'
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',@serviceUrl, 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
exec @returnCode = sp_oamethod @Object, 'read', @ResponseText out, -1
Create table #tmp(dt xml)
insert into #tmp
exec @hr = sp_OAGetProperty @Object, 'responseXML.XML'
Set @ResponseText = Convert(varchar(max), (SELECT dt from #tmp))
Drop Table #tmp
select REPLACE(SUBSTRING(@ResponseText,PATINDEX('%<TravelDistance>%',@ResponseText),21),'<TravelDistance>','') Miles
,REPLACE(REPLACE(SUBSTRING(@ResponseText,PATINDEX('%<TravelDuration>%',@ResponseText),21),'<TravelDuration>',''),'<','')/60 TravelMinutes
Above is not the complete code; I've edited it severely to make it easier to read.
I'm sure there is a better way to do it but this works and it's very exciting.
However, the plan was to create a function that could be used along with other ad-hoc queries to retrieve the same data. Not possible as a function because of the temp table or populating a table from within the function.
So does anyone have a way to make this possible?