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.
--------------------------------------------------------------------------------
(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:
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
分享到:
相关推荐
在本场景中,标题提到的"自动备份sqlserver JOB 用脚本"指的是利用SQL Server Agent服务创建一个JOB,该JOB会定期运行一个脚本来备份数据库。这种自动化备份的方法能确保数据安全,避免因手动操作失误或遗忘而导致的...
"监控MS SQL的Job运行状况的存储过程"提供了一种强大的解决方案,它可以帮助DBA更有效地跟踪和分析SQL Server Agent Jobs的执行状态。这个工具包含了详细的文档和源代码,非常适合SQL Server使用者学习和应用。 ...
在“SQLSERVER.chm”文件中,你将找到关于这些主题的详细信息,包括概念解释、示例代码和最佳实践。深入学习这些内容,你将能够有效地管理和优化SQL Server数据库,为你的职业生涯打下坚实的基础。无论是开发数据库...
5. **使用SQL Server Agent Job**:这是“delete-sqlserver-log.rar_gotb1r”所涉及的内容。SQL Server Agent是一个强大的任务调度工具,可以创建Job来定期执行清理日志的脚本。例如,创建一个批处理Job,执行上面...
Windows 自动定时执行任务的几种实现方法有使用 Windows 任务计划程序、使用 Windows Service 和使用 SQL Server Agent 的 Job 三种。每种方法都有其优缺,用户可以根据实际情况选择合适的方法来实现自动定时执行...
在实际应用中,可能会使用到一些工具来辅助自动化流程,例如在SQL Server中,我们可以创建作业并配置SQL Server Agent来按计划执行。而在其他数据库系统中,比如MySQL或PostgreSQL,可以编写shell脚本或使用特定工具...
当SQL日志文件过大时,可以通过以下几种方式来处理: 1. **重置日志**:通过`DBCC SHRINKFILE`命令可以尝试减小日志文件的大小,但这只适用于事务日志。它会释放未使用的空间,但不会清除日志内容。在清理前,需...
这里我们将深入探讨几种常用的方法,包括作业(Jobs)和存储过程(Stored Procedures),以及如何在不同的数据库系统中实现这些方法。 一、SQL Server 作业(SQL Server Agent Jobs) 在SQL Server中,可以利用SQL ...
为了实现自动化,可以使用SQL Server Agent创建作业,该作业会按照预定的时间间隔运行备份并删除旧的备份。同样,也可以在Windows的批处理文件和Linux的shell脚本中调用这些命令。你可以定义备份的保留期,例如基于...
本文将详细介绍几种实现方法。 #### 方法一:使用ASPExe组件 - **概述**:ASPExe是一种特殊的组件,可以让ASP脚本脱离IIS环境独立运行。这种方式特别适用于那些需要在后台执行的任务,例如定时任务。 - **安装与...
4. **定时触发器**:虽然不直接属于数据库触发器范畴,但与之相关的概念如Oracle的DBMS_JOB或SQL Server的SQL Agent,可以在指定时间执行任务,类似于计划任务。 在"Lesson 4: More About Triggers"中,我们可能会...