In this blog, I will show step by step how to call a Web Service or Rest API in your T-SQL. explain how its works and why we need to do this. Let’s Go.

I. MSXML?

Microsoft XML Core Services (MSXML) are set of services that allow applications written in JScript, VBScript, and Microsoft development tools to build Windows-native XML-based applications.

Microsoft XML Core Services (MSXML) is a set of services that allow developers to build interoperable XML-centered applications on all platforms supporting XML 1.0.

MSXML is compatible with applications written by various development tools executed under the Windows operating platform, such as Visual Basic and JavaScript. MSXML provides multiple required application services, including document object model (DOM), Simple API for XML (SAX), XML data reduced schema (XDR), and schema object model (SOM).

II. T-SQL means?

T-SQL (Transact-SQL) is a set of programming extensions from Sybase and Microsoft that add several features to the Structured Query Language (SQL), including transaction control, exception and error handling, row processing, and declared variables.

for more info and knowledge about MSSQL just visit this Link or HERE!

LETS’S PROCEED;

III QUERY SET-UP

Here is it,

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

--Code Snippet
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'post',  
                 '<API URL HERE >', -- API URL
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
Select @ResponseText  --
Exec sp_OADestroy @Object

In T-SQL query view

DECLARE

The DECLARE statement initializes a variable by assigning it a name and a data type. The variable name must start with the “@“sign. In this example, the data type of the "@Object" variable is int and “@ResponseText” is Varchar. Then inside the open and close bracket  “()” / Varchar(8000), is the size length of the declared capacity of “@ResponseText“.

you can change it depending on the required length. also, you can set it MAX

Declare @ResponseText as Varchar(MAX);

EXEC or EXECUTE

The Exec command is used to execute like Store Procedure or a SQL string passed to it. You can also use full command EXECUTE which is the same as EXEC.

Mostly exec is used to execute a command like Stored Procedure of a long T-SQL command.

Extended Stored Procedures

These are Extended Stored Procedures, and basically,

sp_OACreate is an extended Stored Procedure that initializes during the execution of creating the function. is like connecting database this is the equal command of open connection.

sp_OAMethod is an extended Stored Procedure that initializes a command to send a request like “GET, POST, PUT and etc.”.

sp_OADestroy is also an Extended Stored Procedure that initializes the closed command. same for close connection it’s initialized that command is done and finish or close connection.

Get Response of Web Service or Rest API

Select @ResponseText 

This part is returning the response of the web services or the rest api.

T-SQL Code

Declare @Object as int;
Declare @ResponseText as Varchar(MAX);

--Code Snippet
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'post', 
                 'https://api.javelupango.com/access/index.php?id=1', -- API URL
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
Select @ResponseText  --
Exec sp_OADestroy @Object

Expected Ouput

“SUCCESSS”

Concern

if you are facing like this error, please see the solution below.

Run this code to your T-SQL command.

sp_configure 'show advanced options', 1 

GO 
RECONFIGURE; 
GO 
sp_configure 'Ole Automation Procedures', 1 
GO 
RECONFIGURE; 
GO 
sp_configure 'show advanced options', 1 
GO 
RECONFIGURE;

Related Topics

19 Replies to “Calling a Web Services or Rest API from T-SQL using MSXML”

  1. Hello! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to start. Do you have any points or suggestions? Thanks

  2. great put up, very informative. I wonder why the opposite specialists of this sector don’t understand this. You should continue your writing. I’m sure, you’ve a great readers’ base already!

  3. I think other web site proprietors should take this website as an model, very clean and wonderful user genial style and design, let alone the content. You are an expert in this topic!

  4. The other day, while I was at work, my sister stole my iphone and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!

  5. Can I just say what a reduction to search out somebody who truly knows what theyre speaking about on the internet. You definitely know the best way to deliver a difficulty to mild and make it important. Extra folks need to read this and perceive this aspect of the story. I cant consider youre not more widespread since you definitely have the gift.

  6. One other issue is when you are in a situation where you would not have a cosigner then you may really want to try to wear out all of your financial aid options. You will find many grants or loans and other free college funding that will supply you with funds to aid with school expenses. Thx for the post.

  7. Normally I do not read article on blogs, but I would like to say that this write-up very forced me to check out and do so! Your writing taste has been amazed me. Thank you, quite great article.

  8. I have been exploring for a little for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this info So i am happy to convey that I’ve a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to do not forget this website and give it a look regularly.

  9. Wow, awesome weblog format! How long have you ever been running a blog for? you made blogging look easy. The entire look of your website is magnificent, let alone the content material!

  10. My brother suggested I might like this web site. He was once totally right. This put up truly made my day. You cann’t believe just how a lot time I had spent for this info! Thanks!

  11. I wasn’t sure I could manage such a lengthy article at the beginning. Your style captured my attention. This was a great piece. Great Article Neil. This article is fantastic. Even though I only read it a couple of times in the past, I did not leave a comment. But, I felt this article merited being mentioned.

  12. each time i used to read smaller posts that
    also clear their motive, and that is also happening with this piece of writing which I am reading now.

  13. Hello, this weekend is nice for me, for the reason that this point
    in time i am reading this wonderful educational paragraph here
    at my house.

  14. It’s remarkable to go to see this site and reading the views of all mates on the topic of this article, while I am also keen of getting experience.

Leave a Reply to Ashleigh Cancel reply

Your email address will not be published. Required fields are marked *