Search code examples
restapissmssqldatatypesvarcharmax

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


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:

enter image description here

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:

enter image description here

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


Solution

  • 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.