ABAP写数据到SAP服务器文件并读取
Posted china_wangxiaowen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABAP写数据到SAP服务器文件并读取相关的知识,希望对你有一定的参考价值。
一、在应用服务器中打开文件OPEN DATASET [options]此句打开文件。如果不指定任何模式选项,则文件将按二进制模式打开。如果系统不能打开文件,则将系统字段SY-SUBRC设置为8,否则SY-SUBRC返回0
二、打开文件读取OPEN DATASET FOR INPUT IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开)
三、打开文件写入OPEN DATASET FOR OUTPUT IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开写入,这种是打开文件完全重写)
四、打开文件追加 OPEN DATASET FOR APPENDING IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开写入,这种是打开文件追加记录)
五、关闭应用服务器上的文件 CLOSE DATASET .
六、删除应用服务器上的文件 DELETE DATASET .
七、向应用服务器上文件上写入数据TRANSFER to [LENGTH ].
八、向应用服务器上文件上读取数据READ DATASET INTO [LENGTH ].
常用事务代码:
1.AL11,服务器上的文件
2.CG3Y 下载文件
3.CG3Z 上传文件
注:以上文字内容来自http://blog.csdn.net/lbxp521/article/details/6741534
以下代码来自http://blog.sina.com.cn/s/blog_4a3450310101jlhg.html
保存内表数据到服务器程序
REPORT ydownvbak. TABLES:vbak,vbap. DATA:BEGIN OF wa_itab , vbeln LIKE vbak-vbeln, vkorg LIKE vbak-vkorg, erdat LIKE vbak-erdat, kunnr LIKE vbak-kunnr, posnr LIKE vbap-posnr, matnr LIKE vbap-matnr, kwmeng LIKE vbap-kwmeng, END OF wa_itab. DATA:itab LIKE wa_itab OCCURS 0 WITH HEADER LINE. DATA:g_file LIKE rlgrap-filename. DATA:s_file LIKE rlgrap-filename. DATA:menge(20). DATA:line(1000) TYPE c. SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE t1. SELECT-OPTIONS:s_erdat FOR vbak-erdat, s_vbeln FOR vbak-vbeln, s_kunnr FOR vbak-kunnr, s_vkorg FOR vbak-vkorg. PARAMETERS:p_client AS CHECKBOX, p_dnfile LIKE rlgrap-filename DEFAULT ‘D:\‘, p_server AS CHECKBOX, p_sefile LIKE rlgrap-filename DEFAULT ‘/usr/sap/tmp/‘. SELECTION-SCREEN END OF BLOCK bl1 . INITIALIZATION. t1 = ‘选择条件‘. START-OF-SELECTION. PERFORM sub_read_data. END-OF-SELECTION. PERFORM sub_write_data. *&---------------------------------------------------------------------* *& Form SUB_READ_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM sub_read_data . SELECT vbak~vbeln vkorg vbak~erdat kunnr posnr matnr kwmeng INTO TABLE itab FROM vbak JOIN vbap ON vbap~vbeln = vbak~vbeln WHERE vbak~vbeln IN s_vbeln AND vbak~erdat IN s_erdat AND vkorg IN s_vkorg AND kunnr IN s_kunnr. ENDFORM. " SUB_READ_DATA *&---------------------------------------------------------------------* *& Form SUB_WRITE_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM sub_write_data . IF p_client = ‘X‘. CONCATENATE p_dnfile ‘SD‘ sy-datum sy-uzeit ‘.TXT‘ INTO g_file. "文本格式 CALL FUNCTION ‘WS_DOWNLOAD‘ EXPORTING filename = g_file filetype = ‘DAT‘ TABLES data_tab = itab. "被下载的内表 ENDIF. IF p_server = ‘X‘. CONCATENATE p_sefile ‘SD‘ sy-datum ‘.txt‘ INTO s_file. "服务器保存的目录和文件名 OPEN DATASET s_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT . "打开文件 if sy-subrc = 0. LOOP AT itab INTO wa_itab. menge = wa_itab-kwmeng. CONCATENATE wa_itab-vkorg wa_itab-kunnr wa_itab-vbeln wa_itab-erdat wa_itab-posnr wa_itab-matnr menge INTO line SEPARATED BY ‘‘. TRANSFER line TO s_file . IF sy-subrc NE 0 . MESSAGE ‘ERROR!!‘ type ‘E‘. ENDIF. CLEAR line . ENDLOOP. CLOSE DATASET s_file. else. MESSAGE ‘OPEN ERROR!!‘ type ‘E‘. endif. ENDIF. ENDFORM. " SUB_WRITE_DATA
读取服务器上的文件:
REPORT YDOWNFILE. data:p_file LIKE rlgrap-filename value ‘/usr/sap/tmp/SD20111130.txt‘. data:p_line(100). OPEN DATASET p_file for INPUT in TEXT MODE ENCODING DEFAULT. if sy-subrc ne 0. MESSAGE ‘读取文件出错!‘ type ‘E‘. endif. do. READ DATASET p_file INTO p_line. if sy-subrc ne 0. exit. else. write:/ p_line. endif. enddo. CLOSE DATASET p_file.
以上是关于ABAP写数据到SAP服务器文件并读取的主要内容,如果未能解决你的问题,请参考以下文章
116SAP导出表结构并保存到Excel,方便写代码时复制粘贴
116SAP导出表结构并保存到Excel,方便写代码时复制粘贴
SAP ABAP SM50的另类用途 - ABAP工作进程对数据库表读取操作的检测