Mega Amazon Sale! Don't Miss Out on Exclusive Deals! Shop Now

Azure RunBook

10/23/2023 12:00 AM by Shivendra in Azure


Azure RunBook to Schedule an Azure SQL Database's Stored Procedure

 

In today's digital age, businesses are constantly looking for ways to optimize their operations, particularly when it comes to managing databases. Azure RunBooks offer a valuable solution to automate tasks within Azure SQL databases. This comprehensive guide will walk you through the process of scheduling an Azure SQL Database's stored procedure using Azure RunBooks.
Best Electronics Item

Getting to Know the Basics

Before we dive into the nitty-gritty of scheduling a stored procedure, let's first establish a clear understanding of some essential concepts.

Azure SQL Database

Azure SQL Database is a fully managed relational database service provided by Microsoft Azure. It offers high availability, robust security, and scalability without the burden of manual database management.

Stored Procedure

A stored procedure is essentially a precompiled collection of one or more Transact-SQL statements that can be executed on-demand. This simplifies code execution, promotes code reuse, and enhances security in your database.

Azure RunBook

Azure RunBook, on the other hand, is a feature within Azure Automation that empowers you to automate a wide range of tasks within your Azure environment. It can execute PowerShell scripts and workflows, making it a versatile tool for managing Azure resources.

Step-by-Step Guide

Now, let's dive into the practical steps for scheduling an Azure SQL Database's stored procedure using Azure RunBooks.

Step 1: Access Azure Portal

  1. Start by logging into your Azure Portal account.

Step 2: Create an Automation Account

  1. In the Azure Portal, navigate to 'All Services' and select 'Automation Accounts.

  2. Click 'Add' to create a new Automation Account.
  3. Fill in the necessary details, such as the account name, subscription, and resource group. Then, click 'Create.'
  4. All credentials should be stored in shared resources so that they can be used in a run book automation script.

Step 3: Create a RunBook

  1. Inside your newly created Automation Account, go to 'Runbooks' and select 'Create a runbook.'

  2. Provide a name for the RunBook and choose 'PowerShell' as the runbook type.
  3. In the 'Edit' pane, you can paste your PowerShell script containing the stored procedure you wish to schedule.
     

    param (

        [string]$ServerName,

        [string]$DatabaseName,

        [string]$Username,

        [string]$Query

    )

    # Retrieve the password from the Automation Credential asset

    $CredentialAssetName = "AssetName" # Replace with the name of your credential asset

    $Credential = Get-AutomationPSCredential -Name $CredentialAssetName

    $Password = $Credential.GetNetworkCredential().Password

    # Define the SQL connection string

    $ConnectionString = "Server=$ServerName;Database=$DatabaseName;User Id=$Username;Password=$Password;"

    try {

        # Create a SqlConnection object

        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection

        $SqlConnection.ConnectionString = $ConnectionString

        # Open the SQL connection

        $SqlConnection.Open()

        # Create a SqlCommand object

        $SqlCommand = $SqlConnection.CreateCommand()

        $sqlCommand.CommandTimeout = 120

        $SqlCommand.CommandText = $Query

        $SqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure

        # Execute the SQL query

        $Result = $SqlCommand.ExecuteNonQuery()

        # Process the query results if needed

        # For example, you can use $Result to check the number of rows affected

        Write-Host "Stored procedure '$Query' executed successfully. Rows affected: $Result"

        # Close the SQL connection

        $SqlConnection.Close()

    }

    catch {

        # Handle any errors that occur during SQL execution

        Write-Host "Error: $_"

        throw

    }

     

  4. Don't forget to save and publish the RunBook.

Note:

1. You will need to provide your servername, database, username, and stored procedure name (query) in the script

2. It is a good idea to test run your script, during which time the stored procedure will itself will run – run your script by clicking Start in the runbook overview page and managing the run in the configuration page of the runbook

Step 4: Schedule the RunBook

  1. Under 'Runbooks,' click on the RunBook you just created.
  2. In the 'Runbook Settings,' navigate to 'Schedules.'
  3. Click 'Link to a new schedule' to specify when and how often the RunBook should run.
  4. Configure the schedule according to your needs, setting the frequency, start time, and any recurrence patterns

 

Conclusion

Did you find this blog easy to follow and helpful to you? Let me know in the comments below.

Disclaimer
The purpose of the code contained in blog is solely for learning and demo purposes. The author will not be held responsible for any failures or damages caused due to any other usage.

smileyHappy Learning yes



Logo

CONTACT US

info@seo4uonly.com

ADDRESS

You may like
our most popular tools & apps