如何创建一个存储过程来启动SQL Server作业?
Answer 1:
您可以执行存储过程sp_start_job
在您的存储过程。
在这里看到: http://msdn.microsoft.com/en-us/library/ms186757.aspx
Answer 2:
-- 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
编辑FYI:您可以使用此之前开始。如果你不想启动它是否正在运行,在存储过程运行作业本:
-- 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
Answer 3:
创建存储过程和运行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
文章来源: How to start SQL Server job from a stored procedure?