sql azure 语法

In this article, we will review on elastic job Agent in Azure SQL and how to configure elastic jobs to run scripts on Azure SQL databases. SQL Server Agent is a powerful component that is used to schedule and execute jobs in SQL server. But in Azure, SQL server agent is available only in managed instances and not in the single databases. To schedule and execute jobs on single databases we have a feature called elastic job agent. This feature is used for scheduling and execute jobs on a single database, all the databases in the server, or on all the databases in an elastic pool.

在本文中,我们将介绍Azure SQL中的弹性作业代理,以及如何配置弹性作业以在Azure SQL数据库上运行脚本。 SQL Server代理是一个功能强大的组件,用于计划和执行SQL Server中的作业。 但是在Azure中,SQL Server代理仅在托管实例中可用,而在单个数据库中不可用。 为了在单个数据库上计划和执行作业,我们具有称为弹性作业代理的功能。 此功能用于在单个数据库,服务器中的所有数据库或弹性池中的所有数据库上计划和执行作业。

The following are the steps involved in creating an elastic job and scheduling it.

以下是创建弹性作业和计划作业的步骤。

  1. Creating an Elastic Job agent 创建弹性作业代理
  2. Creating credentials on the Agent database in Azure SQL 在Azure SQL中在代理数据库上创建凭据
  3. Creating a target group and members 创建目标组和成员
  4. Creating logins on target master and user databases 在目标主数据库和用户数据库上创建登录名
  5. Creating job and job steps 创建工作和工作步骤
  6. Schedule the job to execute it on the target database 计划作业以在目标数据库上执行它

Let’s go over these steps one by one.

让我们一步一步地进行这些步骤。

创建弹性作业代理 (Creating Elastic Job agent)

Pre-requisite: we need to have at least one SQL database with a service level objective of S0 or above.

先决条件 :我们需要至少一个SQL数据库,其服务级别目标为S0或更高。

To create an elastic job agent, Navigate to the Azure portal and type elastic job in the search box. Select the Elastic Job agents in the list as shown in the below image.

若要创建弹性作业代理,请导航到Azure门户,然后在搜索框中键入“弹性作业”。 如下图所示,在列表中选择弹性作业代理

On Elastic Job agents page, click on Add.

在“ 弹性作业代理”页面上,单击添加。

Enter the name of the elastic job agent, choose the subscription. Accept the terms (1). Click on OK (2).

输入弹性工作代理的名称,选择订阅。 接受条款(1)。 单击确定 (2)。

Select the Azure SQL database for an elastic job agent (3). The database with the service level objective of S0 or above is eligible for creating an elastic job agent. Click on Create (4).

选择弹性作业代理的Azure SQL数据库(3)。 服务级别目标为S0或更高的数据库可以创建弹性作业代理。 单击创建 (4)。

Now Elastic job agent creates few objects (table, procedures, etc) on the database you selected. Once the deployment is completed we can proceed with further steps.

现在,弹性作业代理在您选择的数据库上创建了几个对象(表,过程等)。 部署完成后,我们可以继续进行进一步的步骤。

在代理数据库上创建数据库范围的凭证 (Creating database scoped credentials on the agent database)

On the Agent database, execute the following script by replacing the password with the password of your choice. These credentials are used to connect with the target database and execute the scripts. Here we have created two credentials in the Agent database.

在代理数据库上,通过将密码替换为您选择的密码来执行以下脚本。 这些凭据用于与目标数据库连接并执行脚本。 在这里,我们在代理数据库中创建了两个凭证。

Credential “JobRun” is used to connect the Azure SQL target database and execute the script. Credential “MasterCred” is used to connect the master database and enumerate all the database in the server if you are using a server or elastic pool as a target type.

凭据“ JobRun ”用于连接Azure SQL目标数据库并执行脚本。 如果您使用服务器或弹性池作为目标类型,则凭据“ MasterCred ”用于连接master数据库并枚举服务器中的所有数据库。

CREATE MASTER KEY ENCRYPTION BY PASSWORD='Password@123';  CREATE DATABASE SCOPED CREDENTIAL JobRun WITH IDENTITY = 'JobUser',SECRET = 'Password@123';
GOCREATE DATABASE SCOPED CREDENTIAL MasterCred WITH IDENTITY = 'MasterUser',SECRET ='Password@123';GO

创建目标组和成员 (Creating a target group and members)

We need to define the target group and the members. The target type can be a single database, or server, or elastic pool.

我们需要定义目标组和成员。 目标类型可以是单个数据库,服务器或弹性池。

On the Agent database, execute the following script by replacing the server name and the database name to create a target group with a single database as the target type. I commented out “@refresh_credential_name” as we are using Azure SQL single database.

在代理数据库上,通过替换服务器名称和数据库名称来执行以下脚本,以单个数据库作为目标类型来创建目标组。 当我们使用Azure SQL单一数据库时,我注释掉了“ @refresh_credential_name”。

EXEC jobs.sp_add_target_group 'DatabaseGroup1'
GOEXEC jobs.sp_add_target_group_member
'DatabaseGroup1',
@target_type =  N'SqlDatabase',
--@refresh_credential_name='MasterCred',
@server_name='rbcus01.database.windows.net',
@database_name =N'TargetDB'
GO

在目标主数据库和用户数据库上创建登录名 (Creating logins on target master and user databases)

We need to create logins with the same password which we used while creating credentials on the Agent database.

我们需要使用与在代理数据库上创建凭据时使用的密码相同的密码来创建登录名。

On the target server, execute the following script on the master database to create the logins.

在目标服务器上,在master数据库上执行以下脚本以创建登录名。

CREATE LOGIN MasterUser
WITH PASSWORD = 'Password@123'; CREATE LOGIN JobUser
WITH PASSWORD = 'Password@123'; CREATE USER MasterUser
FROM LOGIN MasterUser

Now on each target database execute the following script to create a user. Please note that login must have proper permissions to execute the job successfully. For example, if you are executing a procedure using a job, the login must have sufficient permissions to execute the procedure on the target database. To avoid such permission issues, I add the user to a db_owner role on the target Azure SQL database.

现在,在每个目标数据库上执行以下脚本来创建用户。 请注意,登录名必须具有适当的权限才能成功执行作业。 例如,如果要使用作业执行过程,则登录名必须具有足够的权限才能在目标数据库上执行该过程。 为避免此类权限问题,我将用户添加到目标Azure SQL数据库的db_owner角色。

create user JobUser
from login JobUserALTER ROLE db_owner
ADD MEMBER [JobUser] ;
GO

创建工作和工作步骤。 (Creating job and job steps.)

Now on Agent database, execute the following script to create a job and add the steps to the job. Here I am just using a simple print statement in the command. Replace the job name and command and credentials as per your need.

现在在Agent数据库上,执行以下脚本以创建作业并将步骤添加到该作业。 在这里,我只是在命令中使用一个简单的print语句。 根据需要替换作业名称,命令和凭据。

EXEC jobs.sp_add_job @job_name='Sample T-SQL', @description='Print statement'EXEC jobs.sp_add_jobstep @job_name='Sample T-SQL',
@command=N' print ''hi''',
@credential_name='JobRun',
@target_group_name='DatabaseGroup1'

安排工作 (Scheduling the job)

To manually start the job, execute the following script on the Agent database. Replace the job name with your job name.

要手动启动作业,请在代理数据库上执行以下脚本。 用您的工作名称替换工作名称。

EXEC jobs.sp_start_job 'Sample T-SQL'

To check the status of the job, query the view “job_executions”.

要检查作业的状态,请查询视图“ job_executions”。

select * from jobs.job_executions

You can also view job status in the Azure portal, Navigate All resources. Click on the agent. In agent, page click on Overview.

您还可以在Azure门户的“浏览所有资源”中查看作业状态 单击代理。 在代理中,页面单击概述。

To schedule a job, execute the following script on the Agent database.

要计划作业,请在代理数据库上执行以下脚本。

 EXEC jobs.sp_update_job
@job_name='Sample T-SQL',
@enabled=1,
@schedule_interval_type='Minutes',
@schedule_interval_count=1

Below are the different interval types we can specify for a job.

以下是我们可以为作业指定的不同间隔类型。

  1. ‘Once’ '一旦'
  2. ‘Minutes’ '分钟'
  3. ‘Hours’ '小时'
  4. ‘Days’ '天'
  5. ‘Weeks’ “周”
  6. ‘Months’ “月”

To add the new step to existing job execute the following script on the Agent database. If you are adding more than one steps you need to specify the step name.

要将新步骤添加到现有作业中,请在代理数据库上执行以下脚本。 如果要添加多个步骤,则需要指定步骤名称。

EXEC jobs.sp_add_jobstep @job_name='Sample T-SQL',
@step_name ='Execue procedure',
@command=N' EXEC TM',
@credential_name='myjobcred',
@target_group_name='DatabaseGroup1'

There are few procedures that are created on the Agent database when Elastic Job agent was created. These procedures are used to manage jobs, schedules and target groups.

创建弹性作业代理时,在代理数据库上创建的过程很少。 这些过程用于管理作业,日程表和目标组。

结论 (Conclusion)

In this article, we explored how to create an elastic job agent, configure target groups and jobs to run scripts on the Azure SQL target databases. In case you have any questions, please feel free to ask in the comment section below.

在本文中,我们探讨了如何创建弹性作业代理,配置目标组和作业以在Azure SQL目标数据库上运行脚本。 如果您有任何疑问,请随时在下面的评论部分中提问。

翻译自: https://www.sqlshack.com/azure-sql-elastic-job-agent/

sql azure 语法

sql azure 语法_Azure SQL –弹性作业代理相关推荐

  1. sql azure 语法_Azure SQL –使用Azure自动化的索引表

    sql azure 语法 This article provides an overview of indexing tables in Azure SQL database using Azure ...

  2. sql azure 语法_Azure SQL Server中的CREATE DATABASE语句概述

    sql azure 语法 In this article, we will review CREATE DATABASE statement in the Azure SQL database wit ...

  3. sql azure 语法_Azure SQL Server自动故障转移组

    sql azure 语法 In this article, we will review how to set up auto-failover groups in Azure SQL Server ...

  4. sql azure 语法_Azure SQL数据同步–在Azure SQL数据库之间复制数据和架构更改

    sql azure 语法 In this article, we will review how to configure the sync group to replicate data betwe ...

  5. sql azure 语法_Azure Data Studio中SQL Server Profiler

    sql azure 语法 In this article, we will explore SQL Server Profiler in Azure Data Studio in detail inc ...

  6. sql azure 语法_Azure Kubernetes服务(AKS)–管理SQL Server数据库文件

    sql azure 语法 In this article, we will review on managing database files of SQL Server running on Azu ...

  7. sql azure 语法_Azure中的新SQL数据仓库

    sql azure 语法 介绍 (Introduction) In previous chapters, we taught how to create SQL Databases in Azure. ...

  8. sql azure 语法_Azure Data Studio中SQL代码段

    sql azure 语法 This article will fully cover the code snippet SQL developer productivity feature in Az ...

  9. sql azure 语法_Azure Data Studio中SQL Server架构比较扩展

    sql azure 语法 This article explores the SQL Server Schema Compare extension in the Azure Data Studio. ...

最新文章

  1. Java中break、Continue、reutrn总结
  2. 汇编语言MOVZX和MOVSX指令
  3. 【hadoop】1.简介
  4. Python字符串拼接的十种方式
  5. PostgreSQL增强版命令行客户端(pgcli)
  6. 阿里云产品头条(2018年1月刊)
  7. 获奖者:舒继武,男,清华大学计算机系教授、博士生导师,教育部长江学者特聘教授,国家杰出青年基金获得者,《大数据》杂志编委。...
  8. ST众泰:未与长城汽车签署过任何战略协议
  9. 【渝粤教育】国家开放大学2018年春季 7408-21T多媒体软件制作与使用 参考试题
  10. FORM LOV特性
  11. EventUtil——跨浏览器的事件对象
  12. 华为手机所有图标变黑_华为手机字体白色变黑色 华为图标字体怎么变黑
  13. 白帽子讲web安全读后感1
  14. sublime text 3鼠标闪烁由竖线变为横线怎么处理?
  15. 二极管ss14测量_二极管如何测量好坏
  16. 每日习字|2020-05-30
  17. 关于笔记本突然鼠标无法连接,电脑蓝牙消失的问题
  18. div 中进行左右分离
  19. DICOM中窗宽窗位含义
  20. 中石油acm训练赛NO.9(小X与煎饼)

热门文章

  1. 什么是立即执行函数,有什么作用?
  2. XML DOM Object Model in .NET [2/3]
  3. Github简单使用 - 更新项目
  4. 洛谷P2862 [USACO06JAN]把牛Corral the Cows
  5. vue.js基础知识篇(4):过滤器、class与style的绑定2
  6. css3导航渐变 滑过显示动画
  7. MySQL影响性能的因素
  8. 多线程实战(二)线程同步
  9. eclipse plugins
  10. 查找路径php.ini文件到底在哪里?