You can debug batch jobs by going to 'SM37', type in 'JDBG' in the command line ( no '/' ), put the cursor on the job and press enter - will take you to the job in debug mode.
You can do this only after the job has finished execution. This will simulate the exact background scenario with the same selection screen values as used in the job also sy-batch will set to 'X'.
So type in the transaction code 'JDBG' and place your cursor on the job after It has finished. It will take you to a SAP program in debug mode. Step through this program which is about 10 lines, after this your program will be executed in the debug mode.
Or just set proper breakpoints in your program, it will come to the place you want debug…
Hope it helps. Thanks.
========================================
JOB Debug Method
1.In SM37, select a job that has not yet been started (status 'planned' or 'released'). In addition, set a breakpoint at the point in the source code that you want to analyze when debugging. In SM37, enter "JDBG" in the OK code field, and select ENTER. The selected job is now started in debug mode, and the debugger initially stops in a system program. Select F8 to continue the job up to the next breakpoint.
Caution: After debugging, the job still appears in SM37 as 'planned' or 'released', but the entire job (or, more specifically, a copy of it) ran during debugging, and possible database changes are effective as a result of the job.
2.You can catch a current batch job by using SM37 (Catch active job) or SM50 (Debug program). To do this, you must be logged on to the instance on which the job is running. The job is then stopped, and you can keep it running in the debugger.
3.Use SM36 to create a job with two steps, step 1 with the BTCLOOP report and step 2 with the report to be debugged. Then release the job.
You can then debug the job in Transaction SM50. To do this, exit the endless loop in the BTCLOOP report by changing the variable I.
PROGRAM BTCLOOP MESSAGE-ID BT. "470
DATA:
I TYPE I.
I = 0.
DO.
if i ne 0.
exit.
endif.
ENDDO.
注:有的系统Report BTCLOOP跟这段代码不同,不好跳出来