如何创建存储过程以启动SQL Server作业?
答案 0 :(得分:12)
您可以在 存储过程中执行存储过程sp_start_job
。
答案 1 :(得分:10)
-- 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
OR没有参数:
-- 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
EDIT FYI:您可以使用此PRIOR启动如果您不想启动当前正在运行的作业,请在存储过程中执行此操作:
-- 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
答案 2 :(得分:4)
创建商店程序并在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