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
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.Please post if you find a better solution, it will be much appreciated.
Wrong code:
From REPLICATE in SQL Server docs
Correct code:
For other answers, see For Nvarchar(Max) I am only getting 4000 characters in TSQL?