介绍 (Introduction)

In this article, we will answer FAQs about the SQL Server Agent. We will learn how to create a job, some things about the internal tables used, how to schedule jobs, add PowerShell jobs, cmd jobs, T-SQL jobs and more.

在本文中,我们将回答有关SQL Server代理的常见问题。 我们将学习如何创建作业,有关使用的内部表的一些信息,如何安排作业,添加PowerShell作业,cmd作业,T-SQL作业等等。

In this article, we will answer the following questions:

在本文中,我们将回答以下问题:

  1. What is the SQL Server Agent? 什么是SQL Server代理?
  2. Is the SQL Server Agent included in SQL Server Express Edition? SQL Server Express Edition中是否包含SQL Server代理?
  3. How can I schedule tasks in SQL Server Express Edition? 如何在SQL Server Express Edition中计划任务?
  4. How can I start or restart the SQL Agent service? 如何启动或重新启动SQL Agent服务?
  5. How can I create a simple job to backup my database every day at 9 pm? 我如何创建一个简单的工作来每天晚上9点备份数据库?
  6. How can I create a job that executes the command line (cmd)? 如何创建执行命令行(cmd)的作业?
  7. How can I check if a job fails? 如何检查工作是否失败?
  8. When I execute the command in cmd it works fine, but if I run in the agent it fails. What can be the problem? 当我在cmd中执行命令时,它可以正常工作,但是如果我在代理中运行,它将失败。 可能是什么问题?
  9. Is it possible to run jobs across multiple SQL Servers? 是否可以在多个SQL Server上运行作业?
  10. How can we create alerts using the SQL Agent? 我们如何使用SQL Agent创建警报?
  11. How can we send emails using the SQL Agent? 我们如何使用SQL Agent发送电子邮件?
  12. Where is the SQL Agent information stored? SQL Agent信息存储在哪里?

要求 (Requirements)

首先,我们将安装SQL Server。 在此示例中,我使用的是Developer Edition(SQL Server Express版本不包括SQL Agent)。

入门 (Getting started)

What is the SQL Server Agent?

什么是SQL Server代理?

It is a component of the SQL Server that allows to schedule and program jobs to automate some tasks in SQL Server.

它是SQL Server的组件,允许计划和编程作业以自动执行SQL Server中的某些任务。

Is the SQL Server Agent included in SQL Server Express Edition?

SQL Server Express Edition中是否包含SQL Server代理?

No. SQL Server Express Edition is a free version that does not include the SQL Agent (because it is free).

不可以。SQLServer Express Edition是不包含SQL Agent的免费版本(因为它是免费的)。

How can I schedule tasks in SQL Server Express Edition?

如何在SQL Server Express Edition中计划任务?

You could use the Task Scheduler included in Windows and invoke a batch file with an invocation to the sqlcmd with the command required.

您可以使用Windows附带的Task Scheduler,并使用所需命令通过调用sqlcmd来调用批处理文件。

The following example is a batch file that creates a backup to a SQL Server database:

以下示例是一个批处理文件,该文件创建到SQL Server数据库的备份:

First, create a script named backup.sql file with the backup command:

首先,使用backup命令创建一个名为backup.sql文件的脚本:

BACKUP DATABASE [testdb] TO  DISK = N'C:\sql\test.bak'

Next, we will create a file named backup.bat to invoke the script in sqlcmd:

接下来,我们将创建一个名为backup.bat的文件来调用sqlcmd中的脚本:

sqlcmd -S ServerName\SQLEXPRESS -E -i c:\sql\backup.sql -o
c:\sql\output.txt

Where sqlcmd is the command line and -S is used to specify the SQL Server Instance name, -E is used to connect using the current Windows Account and -i is used to specifying the input which is the script backup and -o is used to show the results of the backup in a file named output.txt. In addition, you will need to invoke the backup.bat in windows scheduler.

其中sqlcmd是命令行,-S用于指定SQL Server实例名称,-E用于使用当前Windows帐户进行连接,-i用于指定输入(即脚本备份),-o用于在名为output.txt的文件中显示备份结果。 另外,您将需要在Windows Scheduler中调用backup.bat。

How can I start or restart the SQL Agent service?

如何启动或重新启动SQL Agent服务?

You can start the SQL Agent Service using the SQL Management Studio:

您可以使用SQL Management Studio启动SQL代理服务:

Also using the SQL Server Configuration Manager:

还使用SQL Server配置管理器:

You can also use the command line using the following command:

您还可以使用以下命令来使用命令行:

NET START SQLSERVERAGENT

How can I create a simple job to backup my database every day at 9 pm?

我如何创建一个简单的工作来每天晚上9点备份数据库?

First, you need to create a new job in the SQL Server Agent and enter a name and optionally a description. Then go the Steps page:

首先,您需要在SQL Server代理中创建一个新作业,并输入名称和描述(可选)。 然后转到“步骤”页面:

In steps, create a new step and add the following T-SQL command to backup the database named testdb in the file test.bak:

在步骤中,创建一个新步骤,并添加以下T-SQL命令来备份文件test.bak中名为testdb的数据库:

BACKUP DATABASE [testdb] TO  DISK = N'C:\sql\test.bak'

Go to schedules page and press the new button:

转到时间表页面,然后按新按钮:

Specify any name for the schedule and in schedule type, select recurring and set it to run daily and at 21:00. Now you have a backup ready to run daily at 9:00 PM.

为日程表指定任何名称,并在日程表类型中,选择“定期重复”并将其设置为每天21:00运行。 现在,您已准备好每天晚上9:00运行备份。

How can I create a job that executes the command line (cmd)?

如何创建执行命令行(cmd)的作业?

When you create a new job and a new step (see the previous question if you need detailed steps) you can invoke the Windows command line (cmd). The following options show how to create a local Windows User and then in a second step we will grant permissions to the database in a second T-SQL Step:

创建新作业和新步骤时(如果需要详细步骤,请参阅上一个问题),您可以调用Windows命令行(cmd)。 以下选项显示了如何创建本地Windows用户,然后在第二步中,我们将在第二个T-SQL步骤中授予数据库权限:

In a job step run this command:

在作业步骤中,运行以下命令:

net user japex mypwd /ADD

This command creates a user named japex with password mypwd.

此命令创建一个名为japex的用户,其密码为mypwd。

In a next step, you can grant sysadmin privileges to the user japex:

在下一步中,您可以为用户japex授予sysadmin特权:

CREATE LOGIN [MYSERVER\japex] FROM WINDOWS WITH DEFAULT_DATABASE=[master]
GO
ALTER SERVER ROLE [sysadmin] ADD MEMBER [MYSERVER \japex]

How can I check if a job fails?

如何检查工作是否失败?

You can right-click the job and check the view history to check when it failed and why it failed:

您可以右键单击该作业,然后查看视图历史记录,以查看该作业何时失败以及为何失败:

You can verify the time and reasons:

您可以验证时间和原因:

When I execute the command in cmd it works fine, but if I run the command in the agent it fails. What can be the problem?

当我在cmd中执行命令时,它可以正常工作,但是如果我在代理中运行命令,它将失败。 可能是什么问题?

A typical problem is the permissions problems. If you have an access denied error in your SQL Agent job, you may need more privileges to run the job.

一个典型的问题是权限问题。 如果您SQL Agent作业中出现拒绝访问错误,则可能需要更多特权才能运行该作业。

By default, the SQL Agent runs with the SQLSERVERAGENT account. This account does not have administrator privileges it does not have permissions in some folders and other Windows objects:

默认情况下,SQL Agent使用SQLSERVERAGENT帐户运行。 该帐户没有管理员特权,在某些文件夹和其他Windows对象中没有权限:

A quick solution is going to the SQL Server Configuration Manager and modifying the account to an administrator or grant privileges to the SQLSERVERAGENT account.

一种快速的解决方案是转到SQL Server配置管理器,然后将该帐户修改为管理员或向SQLSERVERAGENT帐户授予特权。

However, it is not recommended for security reasons, to use an administrator account to the SQL Server Agent because a hacker or someone could use that account to attack your OS and your SQL Server.

但是,出于安全原因,不建议对SQL Server代理使用管理员帐户,因为黑客或某人可能会使用该帐户来攻击您的OS和SQL Server。

A good practice is to create a Proxy. First, you will need to create a Credential:

一个好的做法是创建一个代理。 首先,您需要创建一个凭据:

Specify administrator credentials:

指定管理员凭据:

Now, go to the SQL Server Agent, Proxies and right click on Operating System (CmdExcec) and select New Proxy. We will add a proxy to execute cmd tasks with more privileges:

现在,转到“ SQL Server代理,代理”,然后右键单击“操作系统(CmdExcec)”,然后选择“新建代理”。 我们将添加一个代理来执行具有更多特权的cmd任务:

Also, enter a name and select the credential just created:

此外,输入名称并选择刚创建的凭证:

Finally, in your job step run as the proxy just created.

最后,在您的工作步骤中,以刚创建的代理身份运行。

Is it possible to run jobs across multiple SQL Servers?

是否可以在多个SQL Server上运行作业?

Yes, the following article shows how to run jobs on multiple servers:

是的,以下文章显示了如何在多个服务器上运行作业:

  • How to execute jobs on multiple SQL Servers 如何在多个SQL Server上执行作业

How can we create alerts using the SQL Agent?

我们如何使用SQL Agent创建警报?

The following article shows how to create

以下文章显示了如何创建

  • How to create and configure SQL Server Agent Alerts 如何创建和配置SQL Server代理警报

How can we send emails using the SQL Agent?

我们如何使用SQL Agent发送电子邮件?

The following article show how to work with emails:

以下文章显示了如何使用电子邮件:

  • How to configure database mail in SQL Server 如何在SQL Server中配置数据库邮件

Where is the SQL Agent information stored?

SQL Agent信息存储在哪里?

All the information is stored in the MSDB database. This is a system database that stores the jobs, steps, operators and all the information related.

所有信息都存储在MSDB数据库中。 这是一个系统数据库,用于存储作业,步骤,操作员以及所有相关信息。

The following query shows how to get all the jobs:

以下查询显示如何获取所有作业:

Select * from dbo sys.jobs

For more information about the msdb database, refer to this link:

有关msdb数据库的更多信息,请参考以下链接:

SQL Server system databases – the msdb database

SQL Server系统数据库– msdb数据库

结论 (Conclusion)

To conclude, we can say that the SQL Agent helps a lot to automate different tasks and it has a lot of functionality.

总而言之,我们可以说SQL Agent对自动化不同的任务有很大帮助,并且具有很多功能。

翻译自: https://www.sqlshack.com/faq-and-examples-about-the-sql-server-agent/

有关SQL Server代理的常见问题和示例相关推荐

  1. sql server死锁_如何使用扩展事件和SQL Server代理自动执行SQL Server死锁收集过程

    sql server死锁 介绍 (Introduction) This article is the last one of a series in which we discussed how to ...

  2. 使用SQL Server代理生成计划

    摘要 (Summary) SQL Server Agent allows us to create jobs and assign any number of schedules to them. T ...

  3. 对警报线程池的警报线程_检测和警报SQL Server代理丢失的作业

    对警报线程池的警报线程 摘要 (Summary) While alerting on failed SQL Server Agent jobs is straightforward, being no ...

  4. 如何创建和配置SQL Server代理警报

    介绍 (Introduction) If you have ever wanted to run a job dependent on a certain performance condition, ...

  5. SQL Server代理(8/12):使用SQL Server代理外部程序

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这个系列的上篇文章里,你学习如何使用SQ ...

  6. SQL Server代理(4/12):配置数据库邮件

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在以前的文章里我们看到,SQL Serve ...

  7. SQL Server代理(11/12):维护计划作业

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这一系列的上一篇,我们看了使用代理帐户模 ...

  8. SQL Server代理的阶梯 - 第2级:作业步骤和子系列

    作者:Richard Waymire,2017/10/11(第一版:2011/02/17) 原文链接:http://www.sqlservercentral.com/articles/SQL+Agen ...

  9. sql 查询超时已过期_监视来自SQL Server代理作业的查询超时过期消息

    sql 查询超时已过期 SQL Server provides you with a good solution to automate a lot of your administrative ta ...

最新文章

  1. Matplotlib中中文不显示问题
  2. 工作总结的写作方法与要领
  3. 腾讯成联合国全球合作伙伴,TDSQL如何支撑史上最大规模全球会议
  4. ASP.NET CORE 项目实战 ---图形验证码的实现
  5. git 命令详解和常见问题解决
  6. OpenShift 4 - 利用 File Integrity Operator 实现对集群节点进行入侵检测
  7. spring cloud简介之最好参考
  8. Vim编辑器显示行号且定义tab键为4个空格
  9. python流量分析_python 监控流量
  10. 一些简单的局域网入侵命令
  11. 白鹭引擎 4.0 发布 让重度H5游戏研发更简单
  12. 远程医疗中使用AR眼镜,内窥镜,视频远程诊疗方案
  13. jedis设置过期时间
  14. LeetCode(Python实现)——Easy部分【Day2】
  15. mt6765芯片原理图,mt6765芯片芯片资料
  16. redhat7.6添加中文语言支持
  17. 俄分析中国SH-1火炮性能:兼容各型北约弹药
  18. Delphi:文本朗读器--文本转语音(基于百度TTS语音库)
  19. python库中的ssl.py
  20. AAC文件格式与解码流程(未完待续)

热门文章

  1. linux目标文件链接命令,ld 命令:将目标文件链接为可执行程序 - dute.org
  2. output怎么用_这个功能QQ音乐,网易云音乐都有——用python实现一个音乐检索器...
  3. macOS 10.15安装GDB
  4. 示例 - 10行代码在C#中获取页面元素布局信息
  5. 0MQ 事件驱动 以及 poller
  6. 使用nodejs爬前程无忧前端技能排行(半半成品)
  7. 【uoj207】 共价大爷游长沙
  8. SSHnbsp;整合-nbsp;6nbsp;-nbsp;service_serviceImp…
  9. NHibernate入门实例
  10. java中list、set和map 的区别(转)