Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an API from SQL Server stored procedure

Calling an API from ASP.NET Web Form is very easy.

WebClient wc = new WebClient(); string urlData = wc.DownloadString("http://xxx.xxx.xx.xx/sssss/getResponse.do?ID=testing"); 

But can we call an API from SQL Server stored procedure.

If yes then how can we call an API from a SQL Server stored procedure and how can we get API response?

like image 464
Ashish Rathore Avatar asked Feb 27 '14 11:02

Ashish Rathore


People also ask

Can we call webservice from SQL Server stored procedure?

In earlier versions of Sql, you could use either an extended stored proc or xp_cmdshell to shell out and call a webservice. Not that either of these sound like a decent architecture - but sometimes you have to do crazy stuff. Show activity on this post. You can do it with the embedded VB objects.

Can SQL Server connect to API?

Microsoft SQL Server supports many languages and various client libraries. But nowadays, enterprises are shifting towards more flexible and easy-to-create API-driven solutions that allow them to access the server or data using a single REST API interface.

Can you use SQL for API?

By enabling SQL access in your API, developers can pass queries from these sources directly to the API, quickly creating new data flows. Jerod provides the example of Tableau, a data visualization tool that uses SQL queries to populate visualizations.

Can SQL call web service?

In this blog, you will learn about how to call a Web Service from SQL Server, using SQL stored procedure. In my previous project, I was asked to call Web Services from SQL Server stored procedures. It was done using SQL CLR. By using CLR, we can run and manage the code inside the SQL Server.


2 Answers

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',                  'http://www.webservicex.com/stockquote.asmx/GetQuote?symbol=MSFT', --Your Web Service Url (invoked)                  'false' Exec sp_OAMethod @Object, 'send' Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT  Select @ResponseText  Exec sp_OADestroy @Object 
like image 180
fastobject Avatar answered Sep 25 '22 00:09

fastobject


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',                  'http://overpass-api.de/api/interpreter?data=[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)                  'false' 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'$.tags.name'      ) EXEC sp_OADestroy @Object 

This query will give you 3 results:

1. Catch the error in case something goes wrong (don't 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?)

enter image description here

like image 29
Francesco Mantovani Avatar answered Sep 22 '22 00:09

Francesco Mantovani