Calling an API from SQL Server stored procedure

Calling an API from SQL Server stored procedure

Please see a link for more details.

Declare @Object as Int;
Declare @ResponseText as Varchar(8000);

Code Snippet
Exec sp_OACreate MSXML2.XMLHTTP, @Object OUT;
Exec sp_OAMethod @Object, open, NULL, get,
       , --Your Web Service Url (invoked)
Exec sp_OAMethod @Object, send
Exec sp_OAMethod @Object, responseText, @ResponseText OUTPUT

Select @ResponseText

Exec sp_OADestroy @Object

I worked so much, I hope my effort might help you out.

Just paste this into your SSMS and press F5:

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

Exec @hr=sp_OACreate MSXML2.ServerXMLHTTP.6.0, @Object OUT;
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
Exec @hr=sp_OAMethod @Object, open, NULL, get,
       [out:json];area[name=%22Auckland%22]-%3E.a;(node(area.a)[amenity=cinema];way(area.a)[amenity=cinema];rel(area.a)[amenity=cinema];);out;, --Your Web Service Url (invoked)
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
Exec @hr=sp_OAMethod @Object, send
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
Exec @hr=sp_OAMethod @Object, responseText, @json OUTPUT
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object

INSERT into @json (Json_Table) exec sp_OAGetProperty @Object, responseText
-- select the JSON string
select * from @json
-- Parse the JSON string
SELECT * FROM OPENJSON((select * from @json), N$.elements)
WITH (   
      [type] nvarchar(max) N$.type   ,
      [id]   nvarchar(max) N$.id,
      [lat]   nvarchar(max) N$.lat,
      [lon]   nvarchar(max) N$.lon,
      [amenity]   nvarchar(max) N$.tags.amenity,
      [name]   nvarchar(max) N$     
EXEC sp_OADestroy @Object

This query will give you 3 results:

1. Catch the error in case something goes wrong (dont panic, it will always show you an error above 4000 characters because NVARCHAR(MAX) can only store till 4000 characters)

2. Put the JSON into a string (which is what we want)

3. BONUS: parse the JSON and nicely store the data into a table (how cool is that?)


Calling an API from SQL Server stored procedure

Screams in to the void – just no dont do it. This is a dumb idea.

Integrating with external data sources is what SSIS is for, or write a dot net application/service which queries the box and makes the API calls.

Writing CLR code to enable a SQL process to call web-services is the sort of thing that can bring a SQL box to its knees if done badly – imagine putting the the CLR function in a view somewhere – later someone else comes along not knowing what youve donem and joins on that view with a million row table – suddenly your SQL box is making a million individual webapi calls.

The whole idea is insane.

This doing sort of thing is the reason that enterprise DBAs dont trust developers.

CLR is the kind of great power, which brings great responsibility, and the above is an abuse of it.

Leave a Reply

Your email address will not be published.