-- 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 @[email protected];
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