本文主要是介绍job分流,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
待調用的procedure
/****** Object: StoredProcedure [dbo].[pro_job_test1] Script Date: 2018/11/2 上午 11:27:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[pro_job_test1]
@v_flag nvarchar(16)
as
begin
WAITFOR DELAY '00:01';
insert into job_test2
select * from job_test1 where flag=@v_flag
end
創建job的procedure
USE [gs_ssis]
GO
/****** Object: StoredProcedure [dbo].[pro_job_exec] Script Date: 2018/11/2 上午 11:27:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[pro_job_exec]
@jobname nvarchar(256),
@stepname nvarchar(256),
@execsql nvarchar(256)
as
begin
/****** Object: Job [JOB_124_1] Script Date: 2018/11/2 上午 10:46:38 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]]] Script Date: 2018/11/2 上午 10:46:38 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=@jobname,
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=1,
@description=N'沒有可用的描述。',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'xxxxx', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [JOB_124_1] Script Date: 2018/11/2 上午 10:46:38 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=@stepname,
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=@execsql,
@database_name=N'gs_ssis',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
EXEC msdb.dbo.sp_start_job @job_name=@jobname ---執行job
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
select 1
end
測試
exec [pro_job_exec] @jobname='job_1',@stepname='step_1',@execsql='exec pro_job_test1 @v_flag=''1'' '
exec [pro_job_exec] @jobname='job_2',@stepname='step_2',@execsql='exec pro_job_test1 @v_flag=''2'' '
exec [pro_job_exec] @jobname='job_3',@stepname='step_3',@execsql='exec pro_job_test1 @v_flag=''3'' '
exec [pro_job_exec] @jobname='job_4',@stepname='step_4',@execsql='exec pro_job_test1 @v_flag=''4'' '
这篇关于job分流的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!