3

我使用 sql server 调用 google maps api 并获取 gps 位置的数据。它工作正常,但最近我调用的存储过程因此错误而失败

0x8004271F 
ODSOLE Extended Procedure 
The source data specified for this string or binary column or parameter is too long.

当谷歌返回位置数据的“船载”时,似乎达到了字符限制。

到目前为止,这就是我所呼吁的:

-- Select dbo.fnGetAddressFromGPS('33.83528299,-118.28754585')  

-- -- GO
Declare @GPS VarChar(50)
Set @GPS = '34.55,-118.50'
DECLARE @URL VarChar(4000)
DECLARE @win int 
DECLARE @hr  int 
DECLARE @text varchar(8000)

Set @URL = 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=' + @GPS + '&sensor=false'


Set @URL = 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=34.5566767,-118.50677688&sensor=false'
Set @URL = 'http://maps.googleapis.com/maps/api/geocode/json?  latlng=34.5566767,-118.50677688&sensor=false'

EXEC @hr=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@win OUT 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAMethod @win, 'Open',NULL,'GET',@url,'false'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAMethod @win,'Send'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAGetProperty @win, 'ResponseText',@text OUTPUT
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

EXEC @hr=sp_OADestroy @win 
--IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 



Select @Text As 'GoogleResult'
Return



IF CHARINDEX('ZERO_RESULTS', @text) > 0
Select 'No Address Found'


Declare @i Int
Declare @j Int
Declare @Address VarChar(150)

SELECT @i = CHARINDEX('<formatted_address>', @Text);
SELECT @j = CHARINDEX('</formatted_address>', @Text);

SELECT @i = CHARINDEX('"formatted_address" : "', @Text);
Select @i
Return


If @j-@i-Len('<formatted_address>') <= 0 
Select @Address = 'N/A'
Else
Select @Address = SubString(@text, @i+Len('<formatted_address>'), @j-@i-    Len('<formatted_address>'))

--Select @Address

Select @Address  

我想做的是将从谷歌返回的数据限制在格式化的地址部分,这是我真正需要的,但我在文档中没有找到关于如何做这样的事情的任何地方。我能想到的唯一另一件事是截断结果数据,但我现在也想不通。任何帮助将非常感激。

4

1 回答 1

3

这里有趣的部分是插入表格:

将@resTbl 声明为表(结果 NVARCHAR(MAX))

插入 @resTbl EXEC @HttpWinRequest=sp_OAGetProperty @win, 'ResponseText'

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE dbo.usp_http_get
    /*
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
*/
    @url VARCHAR(8000)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @win int, @HttpWinRequest  int

DECLARE @resTbl  AS TABLE (result NVARCHAR(MAX))



EXEC @HttpWinRequest=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@win OUT

IF @HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo @win
EXEC @HttpWinRequest=sp_OAMethod @win, 'Open',NULL,'GET',@url,'false'

IF @HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo @win EXEC @HttpWinRequest=sp_OAMethod @win,'Send'


IF @HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo @win

INSERT @resTbl EXEC @HttpWinRequest=sp_OAGetProperty @win, 'ResponseText'

IF @HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo @win
EXEC @HttpWinRequest=sp_OADestroy @win

IF @HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo @win

SELECT * FROM @resTbl
END
GO

EXEC usp_http_get @url = 'http://www.yahoo.com' -- varchar(8000)
于 2014-12-28T11:19:57.353 回答