How can I schedule a SQL job in Microsoft Azure SQL Database? - sql

I have one SQL Agent maintenance job which checks the index fragmentation within a database and rebuilds indexes if required.
This is running well in my test server (Microsoft Sql Server 2012). But my production server is in Azure. Now I want to schedule that job to Azure.
SQL Agent does not exist in Azure SQL Database so how can I schedule a SQL job in Azure DB?

Since this question was first asked, there is now another alternative to handle this problem:
Azure Functions
Here are a couple of examples that could easily be modified to call a stored procedure that rebuilds your indexes
Create a function in Azure that is triggered by a timer
Use Azure Functions to connect to an Azure SQL Database
Also see
How to maintain Azure SQL Indexes and Statistics - this page has an example stored procedure for rebuilding your indexes that you can download.
Reorganize and Rebuild Indexes
A few things to keep in mind with Azure functions
They are built on top of Azure Web Jobs SDK and offer additional functionality
There are two different pricing models:
App Service plan (attach it to an existing plan)
Predictable cost model
It puts extra load on the same VM used by your web site
Consumption plan
You get some free processing every month
The default maximum run time is 5 minutes to prevent billing problems, but it can be changed via the host.json file.
Edit September 5, 2021 to add additional information
It should be noted that if you need SQL Agent, you have another option now. I would suggest reading up on Azure SQL Managed Instances. You can see a comparison of Azure SQL to Azure SQL Managed instance here in the Microsoft Documentation. With Azure SQL Managed Instances, your transition to the cloud could be a lot simpler since a lot of the on-premise features you are used to are already there (including SQL Server Agent, DB Mail, etc.).

This feature has been rejected by Microsoft (link no longer available).
To quote their response:
Today in Azure there are several alternatives,
SQL Database Elastic Jobs
https://learn.microsoft.com/en-us/azure/azure-sql/database/elastic-jobs-overview
The Azure job scheduler
http://www.windowsazure.com/en-us/services/scheduler/
The new
preview of Azure Automation
http://azure.microsoft.com/en-us/services/automation/.
SQL Server
in a VM
Option 1 requires an additional dedicated cloud service, which increases cost. Option 2 is free (I think) as long as you don't run more than once per hour.

Azure SQL does not support sql jobs. From documentation:
Microsoft Azure SQL Database does not support SQL Server Agent or
jobs. You can, however, run SQL Server Agent on your on-premise SQL
Server and connect to Microsoft Azure SQL Database.
WebJobs: If you have a website you can create webjob and run it on schedule. See more here
Other alternatives - Scheduling job on SQL Azure

Another option is rovergo, a service that allows you to schedule sql jobs with a cron expression. This is nice because you don't have to create a web job or azure function. You can simply schedule a sql script.
(I'm a developer on rovergo)

You can use Azure automation to schedule jobs on an Azure-DB like the on premise SQL Agent.
See https://azure.microsoft.com/nl-nl/blog/azure-automation-your-sql-agent-in-the-cloud/ for more information.

Available for a couple of years now, Elastic Jobs for azure db...
docs:
https://learn.microsoft.com/en-us/azure/azure-sql/database/job-automation-overview?view=azuresql
tutorial:
https://www.youtube.com/watch?v=JIMgqkXZFOQ
Currently seems to use the 2017 version of the sqlagent sp (or a close approximation), but elastic links are now already pointing to SQL2022 preview which contains a newer version of the agent sps

Related

Import/Export Data from SQL (Azure Virtual Machine) to Azure SQL

Our current web application uses Azure SQL Database. We need to create a scheduled process for importing/exporting data from a SQL database in Azure Virtual Machine to Azure SQl database. And also provide ability to trigger manually.
Currently we have manual scripts with Linked server which needs to be run on both servers.
What are the best options available for clean import/Export data automation (scheduled or manual)? for e.g. Secure Connection between servers, Tools or Applications (other than SSIS)
Could someone please provide guidance.
Thank you,
Vamshi
You can use SQL Data Sync tool to sync data between an Azure SQL Server VM and an Azure SQL database. The sync can be done manually or scheduled with a frequency of days, hours, minutes and seconds.
Of course, you can also use SSIS for that purpose. But you may see intermittent connection failures that can substantially impact data loads.
You can use Azure Data Factory to connect to Azure SQL VM and Azure SQL Database and using the copy data tool export/import data from one to the other, and vice versa.

Run a query automatically SSMS without SQL server agent

I'm a new PowerBI developer.
I am inserting data from the Azure SQL database via SSMS.
I don't have access to a server agent for being able to create jobs.
I just want to run 4-5 queries (1 query = 1 step for my job). Is there a simple way to do this automatically for example?
Well, there's no server agent in Azure SQL DB, so you cannot create jobs as you do in on-prem or on Azure VM installations.
However, you do have Elastic jobs on Azure which can run queries on Azure SQL Database periodically.
See the documentation here.

Migrate on-prem SQL Server database to Azure SQL database

We're in the process of a server migration from an on-prem server (Win2008R2) to Azure PaaS.
To move the DBs, we used the Microsoft Data Migration Assistant (DMA) tool, which worked great and we can connect to the migrated Azure DB via SQL Server Management Studio.
Considering:
Made quite a few changes to the migrated Azure DB (tables, stored procedures, indexes) to work with the apps in Azure
Combined multiple on-prem DBs into one DB in Azure via DMA to save costs
On-prem DB is continually being modified by insert/update operations (multiple tables) during the migration process
Question: what is the best and fastest way to migrate data (all vs missing/updated) considering the above?
I would recommend you to migrate first only the schema of your on-premises databases to Azure SQL Databases and then let Azure SQL Data Sync to migrate the data to Azure and keep it updated on Azure SQL Database.
My suggestion to start with an empty schema on the Azure SQL Database side is because when SQL data Sync finds data on-premises and on Azure it start comparing both databases and that consumes a lot of resources.
On the initial sync SQL Data Sync may consume a lot of resources on the on-premises database server even when having an empty schema on the Azure side, for that you can use SQL Server Resource Governor to cap the CPU used by the data sync sessions in your on premises SQL Server, and this way avoid big performance impact possibly affecting database users.
When you are ready, you can switch your users (gradually or not if SQL Data Sync is on bi-directional mode) to Azure. Once your users have been migrated, you can then remove the member database (the on-premises database) from the SQL Data Sync configuration and stop SQL Data Sync operation.
I disagree with all the answers here.
If you are running on Win2008R2 there is a high chance that you are on an old SQL Server (2008? 2012?) which are both deprecated and unsuitable for Azure SQL Database. And probably the application is also old and not suitable for the Cloud in general. I suggest you a good testing phase.
Here my to do list:
Upgrade SQL Server to SQL Server 2016 on-prem and test if all your queries are still running correctly
Test how ready is your SQL Server to go to Azure SQL Database through Microsoft Data Migration Assistant (DMA) tool or the new Azure SQL Migration extension for Azure Data Studio (came out his month).
Don even think for a second that merging databases will reduce your overall costs. Decide if going multi-tenant or single-tanant not because of the price of the database.
Plan for hours of downtime based on the size of the migration. Don't migrate while your database is modified. Expect downtime. The best way is to take a backup of the day before and then resume the logs.
and test like crazy. This is not gonna be easy because the app is old.
Good luck.
Visual Studio also has a great tool for comparing both schema and data between two databases on different servers.
It can then update the target database with any changes after which you can switch over to use the Azure DB.
This method would require downtime of around 5-30 minutes depending on amount of data, but that might be acceptible depending on your requirements.

How to run a T-SQL query daily on an Azure database

I am trying to migrate a database from a sql server into Azure. This database have 2 rather simple TSQL script that inserts data. Since the SQL Agent does not exist on Azure, I am trying to find an alternative.
I see the Automation thing, but it seems really complex for something as simple as running SQL scripts. Is there any better or at least easier way to do this ?
I was under the impression that there was a scheduller for that for I can't find it.
Thanks
There are several ways to run a scheduled Task/job on the azure sql database for your use case -
If you are comfortable using the existing on-premise sql sever agent you can connect to your azure sql db(using linked servers) and execute jobs the same way we used to on on-premise sql server.
Use Automation Account/Runbooks to create sql jobs. If you see marketplace you can find several examples on azure sql db(backup,restore,indexing jobs..). I guess you already tried it and does not seem a feasible solution to you.
Another not very famous way could be to use the webjobs(under app service web app) to schedule tasks(can use powershell scripts here). The disadvantage of this is you cannot change anything once you create a webjob
As #jayendran suggested Azure functions is definitely an option to achieve this use case.
If some how out of these if you do not have options to work with the sql directly , there is also "Scheduler Job Collection" available in azure to schedule invocation of HTTP endpoints, and the sql operation could be abstracted/implemented in that endpoint. This would be only useful for less heavy sql operations else if the operation takes longer chances are it might time out.
You can use Azure Functions to Run the T-SQL Queries for Schedule use Timely Trigger.
You can use Microsoft Flow (https://flow.microsoft.com) in order to create a programmed flow with an SQL Server connector. Then in the connector you set the SQL Azure server, database name, username and password.
SQL Server connector
There are many options but the ones that you can use to run a T-SQL query daily are these:
SQL Connector options
Execute a SQL Query
Execute stored procedure
You can also edit your connection info in Data --> Connections menu.

SQL Script for SQL Azure

I'm developing an app that uses SQL Azure. I don't have an account to access Windows Azure and I want to start writing SQL script.
Can I use my SQL Server 2008 to test my SQL Azure script?
I would also recommend a SQL Azure account. However, if you dont have access to one, you can create the DB in SQL 2008, then export a SQL Azure compatable script. Then use that script for testing purposes.
Here is a link: http://blogs.msdn.com/b/cesardelatorre/archive/2010/06/04/importing-exporting-data-to-sql-azure-databases-using-bcp-and-sql-scripts.aspx
One thing you should accomodate for when coding for SQL Azure is a failover or retry policy per this article: http://blogs.msdn.com/b/appfabriccat/archive/2010/12/11/sql-azure-and-entity-framework-connection-fault-handling.aspx#comments
Not really, because certain SQL statements are not supported, or partially supported. So unless you are already very familiar with the differences between SQL Azure and SQL Server the general recommendation is to create your scripts against a SQL Azure database.
Opening an Account is really simple. Remember that when you create a SQL Azure database your charges are pro-rated daily. So if you create a development database of 1GB is size (the minimum) you will pay $9.99 per month (plus a really low transfer cost), or "roughly" 33 cents per day. If you create a 1GB database on a Monday and drop it the following Wednesday, you will pay roughly a buck. There are no charges for the master database.
I would agree with Herve. It would likely be best if you actually use SQL Azure as there are some differences between SQL Sever and SQL Azure. You can get a free 30 day account (with no credit card) using the following:
Use this link: http://www.windowsazurepass.com/?campid=9FE3DB53-E4F0-DF11-B2EA-001F29C6FB82
Use this passcode: promo code = DPEWE01
What you want to do is actually the right way, you can create a database in you local SQL Server 2008 instance, I would recommend using latest community edition for this purpose.
Create database, tables.. work on it, then generate a script to later export to SQL Azure.
Make sure to see documentation for pressing changes or things not to use while doing your job that might not be working on SQL Azure.