SSMS and sp_OAMethod: is there a Data Type greater than VARCHAR(8000)?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 15:56:17

问题


Calling a REST service through SSMS is indeed not a great idea.

BTW since Microsoft created the Stored Procedure sp_OAMethod and even Phil Factor from Red Gate show us how to use it I wanted to give it a go.

I wanted to import some data from OpenStreetMap directly into MSSQL, so I copy a good query from here and I adapt it to my whish.

In this example I'm returning all Cinemas in Nelson:

DECLARE @obj AS INT
DECLARE @Uri AS NVARCHAR(4000)
DECLARE @Response AS VARCHAR(8000)

SET @Uri = 'http://overpass-api.de/api/interpreter?data=area[name="Nelson"]->.a;(node(area.a)[amenity=cinema];way(area.a)[amenity=cinema];rel(area.a)[amenity=cinema];);out;'
EXEC sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
EXEC sp_OAMethod @obj, 'open', NULL, 'GET', @Uri, false
EXEC sp_OAMethod @obj, 'send'
EXEC sp_OAGetProperty @obj, 'ResponseText', @Response OUTPUT

SELECT @Response [response] 
EXEC sp_OADestroy @obj

Nice and easy, I can see the REST call response in Postman as well as in SSMS:

Problem starts when I try to retrieve all Cinemas from a bigger city like Auckland:

DECLARE @obj AS INT
DECLARE @Uri AS NVARCHAR(4000)
DECLARE @Response AS VARCHAR(8000)

SET @Uri = 'http://overpass-api.de/api/interpreter?data=area[name="Auckland"]->.a;(node(area.a)[amenity=cinema];way(area.a)[amenity=cinema];rel(area.a)[amenity=cinema];);out;'
EXEC sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
EXEC sp_OAMethod @obj, 'open', NULL, 'GET', @Uri, false
EXEC sp_OAMethod @obj, 'send'
EXEC sp_OAGetProperty @obj, 'ResponseText', @Response OUTPUT

SELECT @Response [response] 
EXEC sp_OADestroy @obj

The REST call is retrieving more data and the variable @Response AS VARCHAR(8000) cannot hold all the data:

Of course I tried to use DECLARE @Response AS VARCHAR(MAX) but this won't help neither.

Shouldn't VARCHAR(MAX) hold 65,535 characters and till 2GB of data?

What should I use instead?

Is there a way to split data and concatenate later?

EDIT: I think I'm getting closer: I can use OPENJSON this way, but I still don't know how to structure the query... any help would be appreciated


回答1:


Wrong code:

DECLARE @str varchar(max)
SET @str = REPLICATE('A', 4000) + REPLICATE('B', 4000) + REPLICATE('C', 4000)
PRINT LEN(@str)

From REPLICATE in SQL Server docs

If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.

Correct code:

DECLARE @str varchar(max)
DECLARE @seed varchar(max) = 'A'

SET @str = REPLICATE(@seed, 4000) + REPLICATE(@seed, 4000) + REPLICATE(@seed, 4000)
PRINT LEN(@str)

--12000

For other answers, see For Nvarchar(Max) I am only getting 4000 characters in TSQL?




回答2:


I'm clapping the hands to myself.

I admit, it's a nightmare solution but it get things done. The solution was to set:

Declare @Response as table(Json_Table nvarchar(max))

This way I created a table with a data type which has nvarchar(max) and now yes, it can hold 65,535 characters and till 2GB of data.

Declare @Object as Int;
DECLARE @hr  int
Declare @Response as table(Json_Table nvarchar(max))

Exec @hr=sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', @Object OUT;
Exec @hr=sp_OAMethod @Object, 'open', NULL, 'get',
                 'http://overpass-api.de/api/interpreter?data=[out:json];area[name="Auckland"]->.a;(node(area.a)[amenity=cinema];way(area.a)[amenity=cinema];rel(area.a)[amenity=cinema];);out;', --Your Web Service Url (invoked)
                 'false'
Exec @hr=sp_OAMethod @Object, 'send'
Exec @hr=sp_OAMethod @Object, 'responseText', @Response OUTPUT

INSERT into @Response (Json_Table) exec sp_OAGetProperty @Object, 'responseText'

select * from @Response

EXEC sp_OADestroy @Object

Please post if you find a better solution, it will be much appreciated.



来源:https://stackoverflow.com/questions/52588498/ssms-and-sp-oamethod-is-there-a-data-type-greater-than-varchar8000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!