How To Call A Stored Procedure From Azure Functions

In this Azure tutorial, we will discuss how to call a stored procedure from Azure Functions. Along with this, we will also discuss a few other topics like Create A Stored Procedure In The Azure SQL Database Using The Azure Portal, Creating an Azure Timer Trigger Function using Visual Studio 2019, and Deploy the Azure Timer Trigger Function to Azure From Visual Studio 2019 and Azure Functions SQL Query.

How To Call A Stored Procedure From Azure Functions? Call a stored procedure from the Azure Function using the below steps

  • Log in to the Azure Portal
  • Navigate to your Azure SQL Database and click on the Query Editor (Preview) option from the left navigation and enter your Credentials and run a SQL query to create a Stored Procedure.
  • Create an Azure Function App in Visual Studio 2019 and use the SQL Database connection string from Azure Portal, SQLConnection, and SQLCommand object to call the Stored Procedure.

How To Call A Stored Procedure From Azure Functions

Well, Let’s start discussing a very important topic How to call a stored procedure from Azure Functions. As part of the development activities, Below are the steps that we are going to do exactly.

  1. Create a stored procedure in the Azure SQL database using the Azure Portal.
  2. Create an Azure Timer Trigger Function using Visual Studio 2019.
  3. Deploy the Azure Timer Trigger Function to Azure From Visual Studio 2019.
  4. Run and Test the Azure Function

Create A Stored Procedure In The Azure SQL Database Using The Azure Portal

The first thing we will do is, we will create a stored procedure in your Azure SQL Database using the Azure Portal. Follow the below steps to create a Stored Procedure in the Azure SQL Database using the Azure Portal.

Login to the Azure Portal (https://portal.azure.com/)

Search for the SQL Database and click on the search result

Create A Stored Procedure In The Azure SQL Database Using The Azure Portal

Once you click on the SQL databases, You can see the lists of Azure SQL databases, then click on the one where you want to create the Stored Procedure. Now, on the SQL Database page, click on the Query editor (preview) link. Enter your Login username and password, and then click on the OK button.

After you click on the OK button, there is a chance that you will get the error Cannot Open Server Requested By The Login Azure SQL. You can fix it by following the same URL, and then try logging in again with your Credentials. This time, you should be able to log in.

How to Create A Stored Procedure In The Azure SQL Database Using The Azure Portal

Let’s create a table named Customers first. In the Query editor, copy and paste the SQL query below and click on the Run button to create the Azure SQL table.

CREATE TABLE Customers (
    customerID int,
    LastName varchar(255),
    FirstName varchar(255)
);

You can see it here, the table Customers got Created Successfully.

How To Call A Stored Procedure From Azure Functions

The next step is to create the Stored Procedure with the insert query with a few hardcoded values. You can copy and paste the below SQL query in the query editor to create a stored procedure in the Azure SQL database

CREATE PROCEDURE CustomersNew
AS
INSERT INTO Customers(customerID, LastName, FirstName) VALUES (15, 'Bijay', 'Sahoo')
GO;

You can see that it created the Stored procedure successfully.

Call A Stored Procedure From Azure Functions

Now our Stored Procedure is ready. The next step is to create the Azure Function from where we will call this stored procedure.

We will create An Azure Timer Trigger Function using Visual Studio 2019.

Create an Azure Timer Trigger Function using Visual Studio 2019

To create an Azure Timer Trigger Function, Follow the below steps

Open the Visual Studio 2019 and click on the Create a new Project button to create an Azure Function Project.

Select Azure Function as the Project template and then click on the next button.

On the Configure your new project window, Provide the Project name, Choose a location to save your Project, and then click on the Create button

Create an Azure Timer Trigger Function using Visual Studio 2019

On the Create a new Azure Functions Application