Link to home
Start Free TrialLog in
Avatar of crundle
crundle

asked on

From SQL get a value from XML tag (xml over 8000 char)

How can you get a single value for an XML response that is over 8000 char. Below is the cose and here is the XML (http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=charleston,sc&sensor=false)

If you change the destination to Boston,MA is works


DECLARE @URL varchar(MAX), @Distance varchar(40)
--Google V3--
 set @URL = 'http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Charleston,sc&sensor=false'

 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', @Obj OUT 
 BEGIN TRY
 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 
EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml', @Response OUT
 --print @Result
 END TRY
 BEGIN CATCH
 SET @ErrorMsg = ERROR_MESSAGE()
 END CATCH

 EXEC @Result = sp_OADestroy @Obj

IF (@ErrorMsg IS NOT NULL) OR (@HTTPStatus <> 200) BEGIN
 SET @ErrorMsg = 'Error in spGeocode: ' + ISNULL(@ErrorMsg, 'HTTP result is: ' + CAST(@HTTPStatus AS varchar(10)))
 RAISERROR(@ErrorMsg, 16, 1, @HTTPStatus)
 RETURN 
 END

SET @XML = CAST(@Response AS XML)
SET @Distance = @XML.value('(/DirectionsResponse/route/leg/distance/text) [1]', 'varchar(40)') 

 SELECT  @Distance AS Distance ,@XML AS XMLResults

Open in new window

Avatar of AmmarR
AmmarR
Flag of Bahrain image

dear

varchar can hold maximum of 8000 bytes, so it wont fit your requirnment

try using nvarchar(max)
or ntext
datatypes
Avatar of crundle
crundle

ASKER

Is it possible to just get the distance?
What is the Max for ntext as the XML is 45000 char?
2Ammar

varchar(max)

max indicates that the maximum storage size is 2^31-1 bytes

2crundle

...
set @URL = 'http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Charleston,sc&sensor=false'

 DECLARE @Response varchar(8000)  <--- varchar(max) ???
 DECLARE @XML xml
....
Avatar of crundle

ASKER

varchar(max) fails and no results, The text, and ntext, are invalid for local variables.
Dear crundle:

i took in your code and tested it and yeah i get your point , its because the xml page is returning a large string value

thats why varchar(8000) doesnt allow it and varchar(max) as well

i read few articles on the net and i think you need to see this
-----------
http://blogs.msdn.com/b/dataaccesstechnologies/archive/2009/10/24/sql-server-shows-invalid-data-when-reading-large-xml-file-using-extended-stored-procedures-sp-oacreate.aspx
------------------------
Memory and Handle Leak When You Use MSXML Inside SQL Server Stored Procedure
http://support.microsoft.com/default.aspx?scid=kb;EN-US;303114

----------------
http://www.microsoft.com/sqlserver/2008/en/us/wp-sql-2008-whats-new-xml.aspx

Microsoft dont recommend your method

Regards
Avatar of crundle

ASKER

ok, but I don't see where any instruction to oversome the problem at hand. Do you know of a different method, maybe?
ASKER CERTIFIED SOLUTION
Avatar of AmmarR
AmmarR
Flag of Bahrain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crundle

ASKER

good direction