设为首页 收藏本站
查看: 423|回复: 0

[经验分享] 启动SQL Server Agent Job的几种不同方式

[复制链接]

尚未签到

发表于 2016-11-3 07:52:22 | 显示全部楼层 |阅读模式
Problem:
Every database person might have come across the situation of maintenance tasks such as backing up of databases, re-indexing tables and other such tasks. We often schedule jobs for such tasks, so that they execute as per the set schedule. But there is sometimes the need for these tasks to be executed “On Demand”. This tip shows various ways of executing such tasks on demand by any user regardless of whether the person is technical or not.

Solution
Let’s say I have a job called “BACKUPTEST” which backups the test databases.  I want to be able to execute the job “On Demand”, so whenever anyone needs to do the backup this can be done. In this article I will show you how you can execute such Jobs easily through various ways.

In this tip we will look at these four methods:

SQL Server Management Studio
T-SQL commands
DMO (Distributed Management Objects)
OSQL
Also, this tip assumes that the jobs have already been setup.


--------------------------------------------------------------------------------

(1) - SQL Server Management Studio

The first way that most people are probably aware of is to use SQL Server Management Studio.

SQL Server Agent is the job scheduling tool for SQL Server.

To execute a job on demand using the GUI, open the SQL Server Agent tree, expand Jobs, select the job you want to run, right click on that job and click ‘Start Job’ and the job will execute.

DSC0000.png

--------------------------------------------------------------------------------

(2) -T-SQL commands

The second way is through a T-SQL statement using the  'sp_start_job' stored procedure which instructs SQL Server Agent to execute a job immediately. It is a stored procedure in the 'msdb' database.

The syntax for the sp_start_job stored procedure is:

sp_start_job
  [@job_name] or [@job_id ]
  [,@error_flag ]
  [,@server_name]
  [,@step_name ]
  [,@output_flag ]


Arguments:

[@job_name] | [@job_id ] Is the name of the job to start. Either job_id or job_name must be specified, but both cannot be specified. job_name is sysname, with a default of NULL.
[@error_flag =] error_flag  Reserved.  
[@server_name =] 'server_name'  Is the target server on which to start the job. server_name is nvarchar(30), with a default of NULL. server_name must be one of the target servers to which the job is currently targeted.
[@step_name =] 'step_name'  Is the name of the step at which to begin execution of the job. Applies only to local jobs. step_name is sysname, with a default of NULL
[@output_flag =] output_flag  Reserved.  

When a job run it will have one of two return codes:

0 (success)
1 (failure)
To run the job ‘BACKUPTEST’ it can be executed by a single T-SQL statement: such as:

EXEC msdb.dbo.sp_start_job 'BACKUPTEST'

--------------------------------------------------------------------------------

(3) -DMO (Distributed Management Objects)

Another way of executing the job is through a VBS script using Distributed Management Objects (DMO).

Here is the basic script syntax.

On Error Goto 0: Main()
Sub Main()
   Set objSQL = CreateObject("SQLDMO.SQLServer")
   ' Leave as trusted connection
   objSQL.LoginSecure = True
   ' Change to match the name of your SQL server
   objSQL.Connect "Enter Server Name Here"
   Set objJob = objSQL.JobServer
   For each job in objJob.Jobs
      if instr(1,job.Name,"Enter Job Name Here") > 0 then
         msgbox job.Name
         job.Start  
         msgbox "Job Started"
      end if
   Next
End Sub


Here is sample executing the "BACKUPTEST" job on server "SQLTEST1".  This uses NT authentication to run this script.

On Error Goto 0: Main()
Sub Main()
   Set objSQL = CreateObject("SQLDMO.SQLServer")
   ' Leave as trusted connection
   objSQL.LoginSecure = True
   ' Change to match the name of your SQL server
   objSQL.Connect "SQLTEST1"
   Set objJob = objSQL.JobServer
   For each job in objJob.Jobs
      if instr(1,job.Name,"BACKUPTEST") > 0 then
         msgbox job.Name
         job.Start  
         msgbox "Job Started"
      end if
   Next
End Sub


This code would then be saved in a file and named something like "RunJob.vbs".  You can then double click on the file to execute it or run the code from a command line.


--------------------------------------------------------------------------------

(4)  - Using osql utility

Lastly, we can start the job using osql commands.

The osql utility allows you to enter T-SQL statements, system procedures, and script files.

Here is the basic script syntax.

osql -S "Enter Server Name Here" -E -Q"exec msdb.dbo.sp_start_job 'Enter Job Name Here'"


Open a command prompt and execute the below osql command in it:, replacing your server name and job name.

osql -S "SQLTEST1" -E -Q"exec msdb.dbo.sp_start_job 'BACKUPTEST'"


The next step is to make a batch file which can be run over and over again.

Open notepad and type the commands as follow:


DSC0001.jpg
Save the file as “job.bat”.

The batch is now ready for use. Just double click on it and it will do the maintenance work without having any knowledge of SQL Server.


--------------------------------------------------------------------------------

Permissions

You might have noticed in all the four solutions the msdb stored procedure ‘sp_start_job’ is used in one way or another.

By default, members of the sysadmin fixed server role can execute this stored procedure.

Other users must be granted one of the following SQL Server Agent fixed database roles in the msdb database:

SQLAgentUserRole
SQLAgentReaderRole
SQLAgentOperatorRole
Members of SQLAgentUserRole and SQLAgentReaderRole can only start jobs that they own.

Members of SQLAgentOperatorRole can start all local jobs including those that are owned by other users.

Members of sysadmin can start all local and multiserver jobs.

For details about the permissions of these roles, see SQL Server Agent Fixed Database Roles. and SQL Server Agent Fixed Database Roles



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/GRANDTREE/archive/2009/04/20/4094960.aspx

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-294833-1-1.html 上篇帖子: 让百万级数据瞬间导入SQL Server 下篇帖子: SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表