翻译:Stairway to SQL Server Agent - Level 2: Job Steps and Subsystems

Original link: https://www.sqlservercentral.com/steps/stairway-to-sql-server-agent-level-2-job-steps-and-subsystems

SQL Server Agent ladder - Level 2: job step and subsystems

                                                                                                    ------- Richard - Waymire, 2017-10-11 (first published: 2011-02-17)

SQL Server Agent job by a series of one or more job steps. The job step allocated to a specific job subsystem, this subsystem identifies the type of work to be executed job step. Each job step runs in a separate security context, although there is one owner for each job, you can determine who can modify the job. This article will focus on job step and subsystems constitute a SQL Server Agent job.

A quick review of the work

The best way to consider the SQL Server Agent job is to serve as a container, it is associated with the implementation of a set of other components required for a given task. The main components of the job is the job steps, plans, alarms and notifications.

When you create a job, the owner will be assigned for the job. As the first stage, by default, the user job owner will be created (in Transact-SQL, sp_add_job system by using the stored procedure or SQL Server Management Studio). Most features of SQL Server Agent assume that you are a sysadmin server role. If so, you or any other member of the sysadmin role can modify the job after job creation. If you want a non-member of the sysadmin role can modify the job, the job owner should change the login name for that user. Please note that, regardless of job title, sysadmin members can change any jobs.

Work steps
as the first stage 1, SQL Server Agent jobs composed of at least one job step. When most people think of a job to perform some work, they really meaning to SQL Server is working steps. Job steps, each step will be to perform the job you want to define the type of operation performed by one of the following operating subsystems:

  •          ActiveX
  •          Operating system (CMDExec)
  •           Powershell
  •          Copy all kinds of tasks
  •          SQL Server Analysis Services (SSAS) command (ie XML / A)
  •          SQL Server Analysis Services(SSAS)查询(MDX)
  •          SQL Server Integration Services (SSIS) package (SQL Server DTS package 2000)
  •          Transact-SQL script (T-SQL)

Let's create a work, as well as the necessary steps to back up the work of the Master database. Create a new job, which I call "Backup Master", as shown in Fig.

Figure 1 - Creating new jobs

Now click the Steps tab to view job steps, and then click New ... to open the New Job Step dialog box (Figure 2).

Figure 2 - Create a new job step

I have filled out the steps in this work, in order to perform an integrity check of the master database before starting the backup. I can do all the things one step, but you want to show the workflow between steps. I gave my step name, "the primary database integrity check", and set the type to Transact-SQL. Database context is the master. I entered a simple command in the text box:

DBCC CHECKDB

对于此对话框,有一些解释。 “类型”下拉列表是选择SQL Server代理子系统的位置。下面将描述可能的子系统。 Transact-SQL作业步骤没有安全代理帐户,因此作业步骤将在作业所有者的上下文中运行。安全代理帐户允许作业步骤与不同用户的安全上下文一起运行,并且通常用于不属于sysadmin角色成员的作业。

您选择的作业子系统将更改对话框其余部分的内容。对于Transact-SQL作业步骤,提供了一个简单的文本框供您在Transact-SQL中键入,就像我在这里所做的那样。每个作业步骤可能具有与之关联的不同子系统。

接下来,单击新作业步骤的“高级”选项卡。您将看到如图3所示的屏幕.On success操作定义了一旦成功完成作业步骤后会发生什么。默认选项是转到下一步,这意味着如果作业中有多个步骤,并且这个步骤有效,请执行下一步。如果单击下拉列表,您将看到其他选项 - 包括退出作业(带有成功或失败通知),或跳转到另一个步骤。请注意,最后一个选项跳到另一个步骤,直到您至少在工作中完成第二步才会显示。

图3 - 新作业步骤高级选项

如果某个步骤因某种原因失败,您可以选择尝试重试作业步骤的次数。您还可以指定每次成功完成作业步骤之间的延迟(以分钟为单位)。下面是失败动作(即作业步骤无法完成或以错误代码结束)。选项与成功时相同,具有不同的默认值(如您所料)。

因为这是Transact-SQL作业步骤类型,所以可以将Transact-SQL命令的任何输出记录到输出文件中(就像sqlcmd中的-o选项一样)。您还可以将结果记录到表(MSDB中的sysjobstepslogs表)。

单击“确定”,然后单击“新建”以向作业添加第二步。假设步骤1没有生成错误,则下一个作业将备份master数据库。这是来自我系统的命令(如图4所示):

BACKUP DATABASE [master] TO DISK =
N'C:\ Program Files \ Microsoft SQL Server \ MSSQL10_50.MSSQLSERVER \ MSSQL \ Backup \ master.bak'
与INIT


图4  - 备份master数据库的第二个作业步骤

由于这是作业的结束,单击“高级”选项卡并选择“成功”操作为“退出作业报告成功。”就是这样 - 您应该能够单击“确定”,并查看已完成的作业步骤,如图5。

图5  - 完成的工作

现在单击“确定”完成作业定义,然后运行作业。 单击以运行作业时,请注意,因为现在有多个步骤要求您启动作业的步骤。从第一步开始运行作业(如图6所示),并在第一步成功运行时观察作业流程,然后转到作业的第二步也是最后一步。

图6  - 步骤启动作业(选择步骤1)

成功完成后,作业将标记为成功并退出(如图7所示)。

图7 - 作业已成功运行

工作子系统

您将使用七个主要作业子系统。此数字不包括复制子系统,因为它们是特殊情况,通常这些作业步骤和作业由复制组件而不是DBA创建。我们将把三个Analysis Services子系统的讨论推迟到下一个级别,因为它们有几个独特的考虑因素。

Transact-SQL脚本(T-SQL)

Transact-SQL子系统非常简单,可能是您将创建的最常见的作业步骤类型。它允许您针对此SQL Server代理实例所附带的SQL Server的本地实例运行Transact-SQL(但不是对Transact-SQL的sqlcmd扩展)。请注意,与Analysis Services子系统不同,您只能连接到本地SQL Server实例。另请注意,Transact-SQL没有代理功能,因此T-SQL的作业步骤始终在作业所有者的安全上下文中运行。

ActiveX脚本

ActiveX子系统允许您运行VBScript,Jscript或其他自定义脚本语言(理论上)。默认情况下,该脚本将在SQL Server代理服务帐户的安全上下文中运行。如果您精通VBScript,这可能是一个方便的子系统,但从SQL Server 2008开始,该子系统已被弃用,您应该使用PowerShell子系统。

操作系统(CmdExec)

CmdExec子系统允许您运行操作系统命令(就像打开命令提示符一样)。这些命令将在SQL Server代理服务帐户的安全上下文中运行。在这里(以及在ActiveX子系统中)要记住的关键是没有用户能够单击或接受任何提示,因此请确保您的脚本无需用户干预即可运行。

PowerShell

PowerShell子系统允许您运行Windows PowerShell 1.0或2.0兼容脚本。与其他脚本子系统一样,默认情况下,该脚本将在SQL Server代理服务帐户的安全上下文中运行。PowerShell非常强大,您应该认真研究增长您对PowerShell的了解。 PowerShell将允许您连接到远程系统,因此这是绕过Transact-SQL子系统连接到SQL Server的远程实例的限制的一种方法。

下篇预告

如您所见,SQL Server代理作业步骤是作业内容的核心问题。有许多不同的子系统可供选择,每个子系统都有不同的功能。在这个系列的下一步中,我将检查代理功能,以增强作业步骤的安全性,以及SQL代理子系统剩下的部分——分析服务。

本文是SQL Server Agent的父楼梯Stairway的一部分。


 


 


 


 

 

 


 


 










 

 


 

 



 

Guess you like

Origin blog.csdn.net/Dennis_t/article/details/92395931