如何创建存储过程来启动 SQL Server 作业?
问问题
64893 次
3 回答
15
-- Create SQL Server Agent job start stored procedure with input parameter
CREATE PROC uspStartMyJob @MyJobName sysname
AS
DECLARE @ReturnCode tinyint -- 0 (success) or 1 (failure)
EXEC @ReturnCode=msdb.dbo.sp_start_job @job_name=@MyJobName;
RETURN (@ReturnCode)
GO
或不带参数:
-- Create stored procedure to start SQL Server Agent job
CREATE PROC StartMyMonthlyInventoryJob
AS
EXEC msdb.dbo.sp_start_job N'Monthly Inventory Processing';
GO
-- Execute t-sql stored procedure
EXEC StartMyMonthlyInventoryJob
编辑仅供参考:如果您不想启动当前正在运行的作业,您可以在启动之前使用它,在您的存储过程中工作:
-- Get run status of a job
-- version for SQL Server 2008 T-SQL - Running = 1 = currently executing
-- use YOUR guid here
DECLARE @job_id uniqueidentifier = '5d00732-69E0-2937-8238-40F54CF36BB1'
EXEC master.dbo.xp_sqlagent_enum_jobs 1, sa, @job_id
于 2013-04-09T16:07:45.713 回答
14
您可以在存储过程中执行存储sp_start_job
过程。
于 2013-04-09T16:05:46.343 回答
6
创建您的存储过程并在您的 proc 中运行作业,如下所示:
DECLARE @JobId binary(16)
SELECT @JobId = job_id FROM msdb.dbo.sysjobs WHERE (name = 'JobName')
IF (@JobId IS NOT NULL)
BEGIN
EXEC msdb.dbo.sp_start_job @job_id = @JobId;
END
于 2015-05-21T15:03:26.343 回答