SAP ABAP布置后台作业-sm36 sm37
(一)通过ABAP程序,布置后台Job一般步骤:
- 打开作业队列(JOB_OPEN)
- 调用(SUBMIT)想在此作业进程中,执行的程序
- 关闭作业进程(JOB_CLOSE)
注意点:
- 函数JOB_OPEN,需要传入Job名字(JOBNAME),函数返回Job编号(JOBCOUNT)
- 调用(SUBMIT)程序时,通过(VIA JOB)和(NUMBER)关键字,告诉系统在哪个Job下执行此程序
- 关闭Job时,函数JOB_CLOSE需传入Job名字(JOBNAME)和Job编号(JOBCOUNT),告诉系统关闭哪个Job
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DATA:lv_name LIKE tbtcjob-jobname, lv_number TYPE tbtcjob-jobcount. MOVE 'JOB_NAME' TO lv_name. CALL FUNCTION 'JOB_OPEN' EXPORTING jobname = lv_name IMPORTING jobcount = lv_number EXCEPTIONS cant_create_job = 1 invalid_job_data = 2 jobname_missing = 3. IF sy-subrc EQ 0. SUBMIT z_xxxxxxx_prog WITH p_1 = p_1 WITH p_2 = p_2 USER sy-uname AND RETURN VIA JOB lv_name NUMBER lv_number. IF sy-subrc EQ 0. CALL FUNCTION 'JOB_CLOSE' EXPORTING jobcount = lv_number_fr jobname = lv_name strtimmed = 'X'. "立即开始 ENDIF. ENDIF.
(二)布置Job的起始时间
正常,我们前台SM36布置Job时,是可以控制Job开始时间的。
程序里是通过(JOB_CLOSE)的参数来实现的:
- 参数STRTIMMED = 'X',表示立即执行此Job,默认为空
- 参数SDLSTRTDT和SDLSTRTTM,可以传入Job执行的日期和时间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
CALL FUNCTION 'JOB_CLOSE' EXPORTING jobcount = lv_number_fr jobname = lv_name SDLSTRTDT = '20180921' SDLSTRTTM = '180000'.
(三)实现前一个Job执行后,当前Job再执行
有些业务场景,需要在上一个Job执行完毕或执行成功时,才执行下一个Job。前台SM36是可实现的。
程序里是通过(JOB_CLOSE)的参数和稍稍改变下步骤来实现:
- 打开(JOB_OPEN)作业A
- 调用(SUBMIT)想在作业A中执行的程序
- 打开(JOB_OPEN)作业B,
- 调用(SUBMIT)想在作业B中执行的程序
- 关闭作业B(JOB_CLOSE)
- 关闭作业A(JOB_CLOSE)
注意点:
- 步骤3中,作业A与作业B的传入名字(JOBNAME)一定要相同,只是返回的编号(JOBCOUNT)不同
- 步骤5中,关闭作业B时,通过参数(PRED_JOBNAME)和(PRED_JOBCOUNT),传入A的作业名和编号,
- 参数(PREDJOB_CHECKSTAT)可设置在A执行成功时,才执行B
- 步骤6中,作业A并不是直接调用(JOB_CLOSE)关闭的,而是中间布置了其他作业B(可以是多个),最后才关闭A
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DATA:lv_name LIKE tbtcjob-jobname, lv_number_a TYPE tbtcjob-jobcount, lv_number_b TYPE tbtcjob-jobcount MOVE 'JOB_NAME' TO lv_name. * 布置初始JOB CALL FUNCTION 'JOB_OPEN' EXPORTING jobname = lv_name IMPORTING jobcount = lv_number_a EXCEPTIONS cant_create_job = 1 invalid_job_data = 2 jobname_missing = 3. IF sy-subrc EQ 0. SUBMIT z_xxxxxxx_prog WITH p_1 = p_1 WITH p_2 = p_2 USER sy-uname AND RETURN VIA JOB lv_name NUMBER lv_number_a. ENDIF. * 布置后续JOB,并关闭 CALL FUNCTION 'JOB_OPEN' EXPORTING jobname = lv_name IMPORTING jobcount = lv_number_b EXCEPTIONS cant_create_job = 1 invalid_job_data = 2 jobname_missing = 3. IF sy-subrc EQ 0. SUBMIT z_xxxxxxx_prog WITH p_1 = p_1 WITH p_2 = p_2 USER sy-uname AND RETURN VIA JOB lv_name NUMBER lv_number_b. IF sy-subrc EQ 0. CALL FUNCTION 'JOB_CLOSE' EXPORTING jobcount = lv_number_b jobname = lv_name * predjob_checkstat = 'X' pred_jobcount = lv_number_a pred_jobname = lv_name. ENDIF. ENDIF. * 关闭初始JOB CALL FUNCTION 'JOB_CLOSE' EXPORTING jobcount = lv_number_a jobname = lv_name strtimmed = 'X'.
Stay hungry Stay foolish