时间:2021-07-01 10:21:17 帮助过:3人阅读
OPEN DATASET dset
FOR { INPUT | OUTPUT | APPENDING | UPDATE }
IN { { BINARY MODE }
| { TEXT MODE ENCODING { DEFAULT
| {UTF-8 [SKIPPING|WITH BYTE-ORDER MARK]}
| NON-UNICODE }
[WITH {NATIVE|SMART|UNIX|WINDOWS} LINEFEED] }
| { LEGACY BINARY MODE [{BIG|LITTLE} ENDIAN]
[CODE PAGE cp] }
| { LEGACY TEXT MODE [{BIG|LITTLE} ENDIAN]
[CODE PAGE cp]
[WITH {NATIVE|SMART|UNIX|WINDOWS} LINEFEED] } }
[AT POSITION pos]
[TYPE attr]
[FILTER opcom]
[MESSAGE msg]
[IGNORING CONVERSION ERRORS]
[REPLACEMENT CHARACTER rc].
打开由dset指定的应用服务器上的文件。
tips: text evirionment
text evirionment是ABAP程序的运行时环境的一部分,由语言、locale和系统内码表组成。同一个内部会话中的所有程序有一个共同的文本环境。默认情况下,内部会话的text environment由登录语言决定,并且也可以被语句set locale指定。当前的text environment包含在系统字段sy-langu中 |
使用OPEN DATASET来实现上传数据到应用服务器,即程序中的output_data子进程:
- <span style="color: #808080;">*</span><span style="color: #808080;">&---------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;">& Form OUTPUT_DATA</span><span style="color: #808080;">
- *</span><span style="color: #808080;">&---------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> 将数据写入到Applacation Server</span><span style="color: #808080;">
- *</span><span style="color: #808080;">----------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> --> p1 text</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> <-- p2 text</span><span style="color: #808080;">
- *</span><span style="color: #808080;">----------------------------------------------------------------------*</span>
- <span style="color: #0000ff;">FORM</span><span style="color: #000000;"> output_data .
- </span><span style="color: #0000ff;">DATA</span> s_file <span style="color: #0000ff;">TYPE</span> rlgrap-<span style="color: #000000;">filename.
- </span><span style="color: #0000ff;">PERFORM</span> generate_filename_in_server <span style="color: #0000ff;">USING</span><span style="color: #000000;"> s_file.
- </span><span style="color: #0000ff;">OPEN</span> <span style="color: #0000ff;">DATASET</span> s_file <span style="color: #0000ff;">FOR</span> INPUT <span style="color: #0000ff;">IN</span> <span style="color: #0000ff;">TEXT</span><span style="color: #000000;"> MODE ENCODING DEFAULT.
- </span><span style="color: #0000ff;">IF</span> sy-subrc = <span style="color: #800080;">0</span><span style="color: #000000;"> .
- </span><span style="color: #0000ff;">LOOP</span> <span style="color: #0000ff;">AT</span> itab <span style="color: #0000ff;">INTO</span><span style="color: #000000;"> wa.
- </span><span style="color: #0000ff;">TRANSFER</span> wa <span style="color: #0000ff;">TO</span><span style="color: #000000;"> s_file.
- </span><span style="color: #0000ff;">IF</span> sy-subrc <> <span style="color: #800080;">0</span><span style="color: #000000;"> .
- </span><span style="color: #0000ff;">MESSAGE</span> <span style="color: #800000;">‘</span><span style="color: #008000;">ERROR!!</span><span style="color: #800000;">‘</span> <span style="color: #0000ff;">TYPE</span> <span style="color: #800000;">‘</span><span style="color: #008000;">E</span><span style="color: #800000;">‘</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDIF</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDLOOP</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">CLOSE</span> <span style="color: #0000ff;">DATASET</span><span style="color: #000000;"> s_file.
- </span><span style="color: #0000ff;">WRITE</span>: /<span style="color: #800000;">‘</span><span style="color: #008000;">写入服务器文件成功</span><span style="color: #800000;">‘</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">PERFORM</span> submit_insert_program <span style="color: #0000ff;">USING</span> s_file. <span style="color: #808080;">"</span><span style="color: #808080;">调用导入程序</span>
- <span style="color: #0000ff;">ELSE</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">WRITE</span>: /<span style="color: #800000;">‘</span><span style="color: #008000;">写入服务器文件失败</span><span style="color: #800000;">‘</span><span style="color: #000000;"> .
- </span><span style="color: #0000ff;">ENDIF</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDFORM</span><span style="color: #000000;">.
- </span><span style="color: #808080;">*</span><span style="color: #808080;">&---------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;">& Form GET_file_name_in_server</span><span style="color: #808080;">
- *</span><span style="color: #808080;">&---------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> text</span><span style="color: #808080;">
- *</span><span style="color: #808080;">----------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> --> p1 text</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> <-- p2 text</span><span style="color: #808080;">
- *</span><span style="color: #808080;">----------------------------------------------------------------------*</span>
- <span style="color: #0000ff;">FORM</span> generate_filename_in_server <span style="color: #0000ff;">USING</span><span style="color: #000000;"> s_file .
- </span><span style="color: #0000ff;">DATA</span> s_name <span style="color: #0000ff;">TYPE</span> <span style="color: #0000ff;">string</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">CALL</span> <span style="color: #0000ff;">FUNCTION</span> <span style="color: #800000;">‘</span><span style="color: #008000;">SO_SPLIT_FILE_AND_PATH</span><span style="color: #800000;">‘</span>
- <span style="color: #0000ff;">EXPORTING</span><span style="color: #000000;">
- full_name </span>=<span style="color: #000000;"> p_source
- </span><span style="color: #0000ff;">IMPORTING</span><span style="color: #000000;">
- stripped_name </span>=<span style="color: #000000;"> s_name
- </span><span style="color: #808080;">*</span><span style="color: #808080;"> FILE_PATH =</span>
- <span style="color: #0000ff;">EXCEPTIONS</span><span style="color: #000000;">
- x_error </span>= <span style="color: #800080;">1</span><span style="color: #000000;">
- OTHERS </span>= <span style="color: #800080;">2</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">IF</span> sy-subrc <> <span style="color: #800080;">0</span><span style="color: #000000;">.
- </span><span style="color: #808080;">*</span><span style="color: #808080;"> Implement suitable error handling here</span>
- <span style="color: #0000ff;">ENDIF</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">GET</span> <span style="color: #0000ff;">TIME</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">CONCATENATE</span> <span style="color: #800000;">‘</span><span style="color: #008000;">/tmp/</span><span style="color: #800000;">‘</span> sy-datum sy-uzeit s_name <span style="color: #800000;">‘</span><span style="color: #008000;">.txt</span><span style="color: #800000;">‘</span> <span style="color: #0000ff;">INTO</span><span style="color: #000000;"> s_file.
- </span><span style="color: #0000ff;">TRANSLATE</span> s_file <span style="color: #0000ff;">TO</span> <span style="color: #0000ff;">LOWER</span> <span style="color: #0000ff;">CASE</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDFORM</span>.
上传到服务器的程序已经实现,现在新建一个程序ztest_import,实现导入到数据库的功能。同样使用OPEN DATASET语句来实现:
- <span style="color: #0000ff;">REPORT</span><span style="color: #000000;"> ztest_import.
- </span><span style="color: #0000ff;">SELECTION-SCREEN</span>: <span style="color: #0000ff;">BEGIN</span> <span style="color: #0000ff;">OF</span> <span style="color: #0000ff;">BLOCK</span><span style="color: #000000;"> blk.
- </span><span style="color: #0000ff;">PARAMETERS</span>: s_file <span style="color: #0000ff;">TYPE</span> rlgrap-<span style="color: #000000;">filename .
- </span><span style="color: #0000ff;">SELECTION-SCREEN</span> <span style="color: #0000ff;">END</span> <span style="color: #0000ff;">OF</span> <span style="color: #0000ff;">BLOCK</span><span style="color: #000000;"> blk.
- </span><span style="color: #0000ff;">DATA</span>: <span style="color: #0000ff;">BEGIN</span> <span style="color: #0000ff;">OF</span><span style="color: #000000;"> wa,
- col1(</span><span style="color: #800080;">30</span>) <span style="color: #0000ff;">TYPE</span><span style="color: #000000;"> c,
- col2(</span><span style="color: #800080;">30</span>) <span style="color: #0000ff;">TYPE</span><span style="color: #000000;"> c,
- col3(</span><span style="color: #800080;">30</span>) <span style="color: #0000ff;">TYPE</span><span style="color: #000000;"> c,
- </span><span style="color: #0000ff;">END</span> <span style="color: #0000ff;">OF</span><span style="color: #000000;"> wa.
- </span><span style="color: #0000ff;">FIELD-SYMBOLS</span>: <hex_container> <span style="color: #0000ff;">TYPE</span><span style="color: #000000;"> c.
- </span><span style="color: #0000ff;">TRANSLATE</span> s_file <span style="color: #0000ff;">TO</span> LOWER <span style="color: #0000ff;">CASE</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">OPEN</span> <span style="color: #0000ff;">DATASET</span> s_file <span style="color: #0000ff;">FOR</span> INPUT <span style="color: #0000ff;">IN</span> <span style="color: #0000ff;">TEXT</span><span style="color: #000000;"> MODE ENCODING DEFAULT.
- </span><span style="color: #0000ff;">IF</span> sy-subrc = <span style="color: #800080;">0</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ASSIGN</span> wa <span style="color: #0000ff;">TO</span> <hex_container><span style="color: #000000;"> CASTING.
- </span><span style="color: #0000ff;">DO</span><span style="color: #000000;">.
- </span><span style="color: #808080;">"</span><span style="color: #808080;">由于没有使用MAXIMUM LENGTH选项,所以每次读取的最大字节数</span>
- <span style="color: #808080;">"</span><span style="color: #808080;">由<hex_container>所占字节数决定</span>
- <span style="color: #0000ff;">READ</span> <span style="color: #0000ff;">DATASET</span> s_file <span style="color: #0000ff;">INTO</span> <hex_container><span style="color: #000000;">.
- </span><span style="color: #0000ff;">IF</span> sy-subrc = <span style="color: #800080;">0</span><span style="color: #000000;">.
- </span><span style="color: #808080;">*</span><span style="color: #808080;">"ztestly是在SE11中新建的表,结构和上传的excel相符,过程省略</span>
- <span style="color: #0000ff;">INSERT</span> ztestly <span style="color: #0000ff;">FROM</span><span style="color: #000000;"> wa.
- </span><span style="color: #0000ff;">WRITE</span><span style="color: #000000;"> wa.
- </span><span style="color: #0000ff;">ELSE</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">EXIT</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDIF</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDDO</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">CLOSE</span> <span style="color: #0000ff;">DATASET</span><span style="color: #000000;"> s_file.
- </span><span style="color: #0000ff;">DELETE</span> <span style="color: #0000ff;">DATASET</span> s_file. <span style="color: #808080;">"</span><span style="color: #808080;">成功执行导入后,删除服务器上的文件</span>
- <span style="color: #0000ff;">ELSE</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">MESSAGE</span> <span style="color: #800000;">‘</span><span style="color: #008000;">open failed</span><span style="color: #800000;">‘</span> <span style="color: #0000ff;">TYPE</span> <span style="color: #800000;">‘</span><span style="color: #008000;">I</span>
如果你看过我博客中有关submit的文章,可能会记得,submit语句是可以安排程序作为后台作业进行的。下面我们就使用这个特性,在上传程序ztest_upload的子程序submit_insert_program中调用导入程序ztest_import,代码如下:
- <span style="color: #808080;">*</span><span style="color: #808080;">&---------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;">& Form SUBMIT_INSERT_PROGRAM</span><span style="color: #808080;">
- *</span><span style="color: #808080;">&---------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> text</span><span style="color: #808080;">
- *</span><span style="color: #808080;">----------------------------------------------------------------------*</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> --> p1 text</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> <-- p2 text</span><span style="color: #808080;">
- *</span><span style="color: #808080;">----------------------------------------------------------------------*</span>
- <span style="color: #0000ff;">FORM</span> submit_insert_program <span style="color: #0000ff;">USING</span><span style="color: #000000;"> s_file.
- </span><span style="color: #0000ff;">DATA</span>: w_jobid <span style="color: #0000ff;">TYPE</span> tbtcjob-<span style="color: #000000;">jobcount,
- w_stepnm </span><span style="color: #0000ff;">TYPE</span> tbtcjob-<span style="color: #000000;">stepcount,
- p_job_nm </span><span style="color: #0000ff;">TYPE</span> tbtcjob-jobname VALUE <span style="color: #800000;">‘</span><span style="color: #008000;">ZTEST_IMPORT</span><span style="color: #800000;">‘</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">CLEAR</span><span style="color: #000000;">:w_jobid.
- </span><span style="color: #0000ff;">CALL</span> <span style="color: #0000ff;">FUNCTION</span> <span style="color: #800000;">‘</span><span style="color: #008000;">JOB_OPEN</span><span style="color: #800000;">‘</span>
- <span style="color: #0000ff;">EXPORTING</span>
- <span style="color: #808080;">*</span><span style="color: #808080;"> DELANFREP = ‘ ‘</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> JOBGROUP = ‘ ‘</span>
- jobname =<span style="color: #000000;"> p_job_nm
- </span><span style="color: #808080;">*</span><span style="color: #808080;"> SDLSTRTDT = NO_DATE</span><span style="color: #808080;">
- *</span><span style="color: #808080;"> SDLSTRTTM = NO_TIME</span>
- <span style="color: #0000ff;">IMPORTING</span><span style="color: #000000;">
- jobcount </span>=<span style="color: #000000;"> w_jobid
- </span><span style="color: #0000ff;">EXCEPTIONS</span><span style="color: #000000;">
- cant_create_job </span>= <span style="color: #800080;">1</span><span style="color: #000000;">
- invalid_job_data </span>= <span style="color: #800080;">2</span><span style="color: #000000;">
- jobname_missing </span>= <span style="color: #800080;">3</span><span style="color: #000000;">
- OTHERS </span>= <span style="color: #800080;">4</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">SUBMIT</span> ztest_import <span style="color: #0000ff;">WITH</span> s_file = s_file VIA JOB p_job_nm <span style="color: #0000ff;">NUMBER</span> w_jobid <span style="color: #0000ff;">AND</span><span style="color: #000000;"> RETURN.
- </span><span style="color: #0000ff;">IF</span> sy-subrc = <span style="color: #800080;">0</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">CALL</span> <span style="color: #0000ff;">FUNCTION</span> <span style="color: #800000;">‘</span><span style="color: #008000;">JOB_CLOSE</span><span style="color: #800000;">‘</span>
- <span style="color: #0000ff;">EXPORTING</span><span style="color: #000000;">
- jobcount </span>=<span style="color: #000000;"> w_jobid
- jobname </span>=<span style="color: #000000;"> p_job_nm
- strtimmed </span>= <span style="color: #800000;">‘</span><span style="color: #008000;">X</span><span style="color: #800000;">‘</span>
- <span style="color: #0000ff;">EXCEPTIONS</span><span style="color: #000000;">
- cant_start_immediate </span>= <span style="color: #800080;">1</span><span style="color: #000000;">
- invalid_startdate </span>= <span style="color: #800080;">2</span><span style="color: #000000;">
- jobname_missing </span>= <span style="color: #800080;">3</span><span style="color: #000000;">
- job_close_failed </span>= <span style="color: #800080;">4</span><span style="color: #000000;">
- job_nosteps </span>= <span style="color: #800080;">5</span><span style="color: #000000;">
- job_notex </span>= <span style="color: #800080;">6</span><span style="color: #000000;">
- lock_failed </span>= <span style="color: #800080;">7</span><span style="color: #000000;">
- OTHERS </span>= <span style="color: #800080;">8</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">IF</span> sy-subrc <> <span style="color: #800080;">0</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">WRITE</span>: /<span style="color: #800000;">‘</span><span style="color: #008000;">后台作业发布失败</span><span style="color: #800000;">‘</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ELSE</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">WRITE</span>: /<span style="color: #800000;">‘</span><span style="color: #008000;">后台作业发布成功</span><span style="color: #800000;">‘</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDIF</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDIF</span><span style="color: #000000;">.
- </span><span style="color: #0000ff;">ENDFORM</span>.
运行zterst_upload,上传一个有效的excel文件,我们便可以在上一篇文章提到的自有作业界面看到后台作业执行成功,并且在SE16中查到自建表zestly中已经插入了相关数据。如果要改变后台作业的计划时间,可以在JOB_OPEN函数中设置相关参数。
一步步实现ABAP后台导入EXCEL到数据库【2】
标签: