ABAP:从例子学习ABAP
1、插入内表行:
*插入内表行:
DATA: BEGIN OF man,
name(20) TYPE c,
high TYPE p DECIMALS 2,
weight TYPE p DECIMALS 2,
END OF man. DATA: man1 LIKE TABLE OF man. man-name = '张参'.
man-high = '1.68'.
man-weight = 120.
INSERT man INTO TABLE man1. man-name = '刘志'.
man-high = '1.78'.
man-weight = 160.
INSERT man INTO TABLE man1. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
ULINE. man-name = '钱华'.
man-high = '2.22'.
man-weight = 220.
INSERT man INTO man1 INDEX 3. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP. ULINE.
man-name = '钱华'.
man-high = '2.22'.
man-weight = 220.
INSERT man INTO man1 INDEX 1. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
2、读取文件
2.1、使用upload函数
*读放文件
DATA: BEGIN OF man,
name(20) TYPE c,
high TYPE p DECIMALS 2,
weight TYPE p DECIMALS 2,
END OF man. DATA: man1 LIKE TABLE OF man. CALL FUNCTION 'UPLOAD'
EXPORTING
codepage = 'test'
filename = 'd:\temp\testa.txt'
filetype = 'dat'
item = '读取文件'
TABLES
data_tab = man1
EXCEPTIONS
invalid_filesize = 1
invalid_table_width = 2
invalid_type = 3
no_batch = 4
unknown_error = 5
gui_refuse_filetransfer = 6
OTHERS = 7.
IF sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
2.2、使用WS_UPLOAD函数
*读取文件
DATA: BEGIN OF man,
name() TYPE c,
high TYPE p DECIMALS ,
weight TYPE p DECIMALS ,
END OF man. DATA: man1 LIKE TABLE OF man. CALL FUNCTION 'WS_UPLOAD'
EXPORTING
codepage = 'test'
filename = 'd:\temp\testa.txt'
filetype = 'DAT'
TABLES
data_tab = man1
EXCEPTIONS
invalid_filesize =
invalid_table_width =
invalid_type =
no_batch =
unknown_error =
gui_refuse_filetransfer =
OTHERS = .
IF sy-subrc <> .
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
2.3、使用GUI_UPLOAD函数
*直接读取文件
DATA: BEGIN OF man,
name() TYPE c,
high TYPE p DECIMALS ,
weight TYPE p DECIMALS ,
END OF man. DATA: man1 LIKE TABLE OF man. CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = 'd:\temp\testa.txt'
FILETYPE = 'DAT'
* HAS_FIELD_SEPARATOR = ' '
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
tables
data_tab = man1
* CHANGING
* ISSCANPERFORMED = ' '
* EXCEPTIONS
* FILE_OPEN_ERROR = 1
* FILE_READ_ERROR = 2
* NO_BATCH = 3
* GUI_REFUSE_FILETRANSFER = 4
* INVALID_TYPE = 5
* NO_AUTHORITY = 6
* UNKNOWN_ERROR = 7
* BAD_DATA_FORMAT = 8
* HEADER_NOT_ALLOWED = 9
* SEPARATOR_NOT_ALLOWED = 10
* HEADER_TOO_LONG = 11
* UNKNOWN_DP_ERROR = 12
* ACCESS_DENIED = 13
* DP_OUT_OF_MEMORY = 14
* DISK_FULL = 15
* DP_TIMEOUT = 16
* OTHERS = 17
.
IF sy-subrc <> .
* Implement suitable error handling here
ENDIF. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
2.4、读取文件小例子
DATA file_table TYPE file_table. "引用FILE_TABLE对象 DATA: lt_file_names TYPE filetable,
lwa_file_name LIKE LINE OF lt_file_names,
lv_subrc TYPE i. SELECT-OPTIONS: i_file FOR file_table NO INTERVALS. AT SELECTION-SCREEN ON VALUE-REQUEST FOR i_file-low.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
* WINDOW_TITLE =
* DEFAULT_EXTENSION =
* DEFAULT_FILENAME =
* FILE_FILTER =
initial_directory = 'd:\temp'
multiselection = 'X'
CHANGING
file_table = lt_file_names[]
rc = lv_subrc "打开文件的数量
* USER_ACTION =
EXCEPTIONS
file_open_dialog_failed =
cntl_error =
error_no_gui =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. i_file-sign = 'I'.
i_file-option = 'EQ'. LOOP AT lt_file_names INTO lwa_file_name.
i_file-low = lwa_file_name.
APPEND i_file.
ENDLOOP. START-OF-SELECTION.
WRITE:'打开文件的数量:',lv_subrc LEFT-JUSTIFIED.
WRITE: / '打开文件的数量:',LINES( i_file ) LEFT-JUSTIFIED.
LOOP AT i_file.
WRITE: / i_file-low.
ENDLOOP.
3、保存文件
*保存文件
DATA: BEGIN OF man,
name(20) TYPE c,
high TYPE p DECIMALS 2,
weight TYPE p DECIMALS 2,
END OF man. DATA: man1 LIKE TABLE OF man.
DATA: name TYPE rlgrap-filename, typa TYPE rlgrap-filetype. man-name = '张参'.
man-high = '1.68'.
man-weight = 120.
INSERT man INTO TABLE man1. man-name = '刘志'.
man-high = '1.78'.
man-weight = 160.
INSERT man INTO TABLE man1. man-name = '钱华'.
man-high = '2.22'.
man-weight = 220.
INSERT man INTO man1 INDEX 2. name = 'd:\temp\testad.txt'.
typa = 'dat'. CALL FUNCTION 'DOWNLOAD'
EXPORTING
codepage = 'testa'
filename = name
filetype = typa
item = '文件测试'
TABLES
data_tab = man1
EXCEPTIONS
invalid_filesize = 1
invalid_table_width = 2
invalid_type = 3
no_batch = 4
unknown_error = 5
gui_refuse_filetransfer
=6
OTHERS = 7.
IF
sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
4、格式化输出
DATA wa LIKE spfli.
WRITE:/. WRITE:10'航班承运人',40'航班连接',60'国家代码',80'起飞城市',100'起飞机场'.
5、内表排序
*内表排序
DATA: BEGIN OF man,
name(20) TYPE c,
high TYPE p DECIMALS 2,
weight TYPE p DECIMALS 2,
END OF man. DATA: man1 LIKE hashed TABLE OF man with unique key name. man-name = '张参'.
man-high = '1.68'.
man-weight = 120.
INSERT man INTO TABLE man1. man-name = '刘志'.
man-high = '1.78'.
man-weight = 160.
INSERT man INTO TABLE man1. man-name = '钱华'.
man-high = '2.22'.
man-weight = 220.
INSERT man INTO table man1. LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
ULINE. sort man1 ascending by weight descending .
skip.
uline.
LOOP AT man1 INTO man.
WRITE: / man-name,man-high,man-weight.
ENDLOOP.
6、消息
*消息
*--开始
*DATA: msg1(20) TYPE c VALUE '测试'.
*DATA: msg2(20) TYPE c VALUE 'ABAP'.
*DATA: msg3(20) TYPE c VALUE '消息!'.
*
*MESSAGE i006(ymess) WITH msg1 msg2 msg3.
*
*data:BEGIN OF man,
* name(30) TYPE c,
* high TYPE p DECIMALS 2,
* weight TYPE p DECIMALS 2,
*END OF man.
*
*FIELD-SYMBOLS <fsa> LIKE man.
*DATA man1 LIKE man.
*
*man-name = '雨荷'.
*man-high = '1.78'.
*man-weight = 140.
*
*ASSIGN man TO <fsa>.
*WRITE:/ <fsa>-name,
* man-high,
*<fsa>-weight.
*--结束 *--开始
*DATA: msg1(20) TYPE c VALUE '测试'.
*DATA: msg2(20) TYPE c VALUE 'ABAP'.
*DATA: msg3(20) TYPE c VALUE '消息!'.
*
*MESSAGE i001(00) WITH msg1 msg2 msg3.
*
*
*DATA: c1(2) TYPE c,
* c2(2) TYPE c,
* c3(2) TYPE c,
* c4(2) TYPE c,
* c5(20) TYPE c VALUE '91* 12*23*344445',
* c9(2) TYPE c.
*
*c9 = '* '.
*WRITE: / .
*WRITE c5.
*SPLIT c5 AT c9 INTO c1 c2 c3 c4 .
*WRITE: / c1,c2,c3,c4.
*--结束 *--开始
*DATA: msg1(20) TYPE c VALUE '测试'.
*DATA: msg2(20) TYPE c VALUE 'ABAP'.
*DATA: msg3(20) TYPE c VALUE '消息!'.
*
*MESSAGE i001(00) WITH msg1 msg2 msg3.
*
*
*DATA: BEGIN OF man,
* name(20) TYPE c,
* high TYPE p DECIMALS 2,
* weight TYPE p DECIMALS 2,
*END OF man.
*
*DATA: man1 LIKE TABLE OF man WITH HEADER LINE,
*man2 LIKE TABLE OF man.
*
*man-name = '张参'.
*man-high = '1.68'.
*man-weight = 120.
*APPEND man TO man1.
*
*man-name = '刘志'.
*man-high = '1.78'.
*man-weight = 160.
*APPEND man TO man1.
*
*MOVE man1[] TO man2.
*
*
* WRITE: / man-name,man-high,man-weight.
*
*
*LOOP AT man1.
*write: / man1-name,man1-high,man1-weight.
*ENDLOOP.
*--结束
7、读取数据表
**读取数据表
*定义工作区
DATA a_spfli TYPE spfli.
*定义内表,请注意有Header line
DATA ta_spfli TYPE TABLE OF spfli WITH HEADER LINE.
*分隔线
ULINE.
WRITE / '使用工作区'.
ULINE.
* 将数据表逐行转移至工作区,只读前5行
SELECT * INTO CORRESPONDING FIELDS OF
a_spfli FROM spfli UP TO ROWS.
WRITE: / a_spfli-connid,a_spfli-carrid,a_spfli-cityfrom,
a_spfli-cityto.
ENDSELECT. ULINE.
WRITE / '使用内表'.
ULINE.
SELECT * INTO CORRESPONDING FIELDS OF
TABLE ta_spfli FROM spfli UP TO ROWS.
*从数据表读数据至内表,直接从内表输出
*如果TA_SPFLI没有定义Header line,会出错
LOOP AT ta_spfli.
WRITE: / a_spfli-connid,a_spfli-carrid,a_spfli-cityfrom,
a_spfli-cityto.
ENDLOOP.
ULINE.
WRITE / '将内表数据转移至工作区'.
ULINE.
LOOP AT ta_spfli INTO a_spfli.
WRITE: / a_spfli-connid,a_spfli-carrid,a_spfli-cityfrom,
a_spfli-cityto.
ENDLOOP.
8、内连接和外连接
*内连接和外连接
*定义结构
DATA: BEGIN OF arcd,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
fldate TYPE sflight-fldate,
END OF arcd.
*工作区
DATA arcd1 LIKE arcd.
*关联数据表,读取前10条记录,写入工作区,并输出
SELECT spfli~carrid spfli~connid sflight~fldate INTO arcd1 FROM spfli
INNER JOIN sflight ON spfli~carrid = sflight~carrid
AND spfli~connid = sflight~connid UP TO ROWS.
WRITE: / arcd1-carrid,arcd1-connid,arcd1-fldate.
ENDSELECT.
9、使用package size 读取数据
**使用package size 读取数据
*定义内表,请注意有Header line
DATA wa_spfli TYPE TABLE OF spfli WITH HEADER LINE.
*分隔线
ULINE.
* 以每次读取5条记录,直到读取所有数据
SELECT * INTO TABLE wa_spfli FROM spfli PACKAGE SIZE .
*输出内表记录
LOOP AT wa_spfli.
WRITE: / wa_spfli-connid,wa_spfli-carrid,wa_spfli-cityfrom,
wa_spfli-cityto.
ENDLOOP.
*输出5条记录后输出一条横线
ULINE.
ENDSELECT.
10、交互式列表
**交互式列表
START-OF-SELECTION.
WRITE:'初如列表,sy-lsind = ' ,sy-lsind. AT LINE-SELECTION.
IF sy-lsind = .
WRITE: '第二列表,sy-lsind =',sy-lsind.
ENDIF. AT LINE-SELECTION.
IF sy-lsind = .
WRITE: '第三列表,sy-lsind =',sy-lsind.
ENDIF.
11、列表颜色
**列表颜色
*数据库表spfli相关
TABLES spfli.
SKIP. *输出表头,背景灰蓝
FORMAT COLOR COL_HEADING.
ULINE AT /(). WRITE: / sy-vline,() '航线承运人', sy-vline , () '航班连接', sy-vline,
() '国家代码', sy-vline,() '起飞城市', sy-vline,
() '起飞机场',sy-vline.
ULINE AT /().
FORMAT COLOR OFF.
*输出内容,输出关键值为‘LH’为红色
SELECT * FROM spfli.
IF spfli-carrid = 'LH'.
FORMAT COLOR COL_NEGATIVE.
ELSE.
FORMAT COLOR OFF.
ENDIF.
WRITE: / sy-vline,() spfli-carrid,sy-vline,() spfli-connid,sy-vline,
() spfli-countryto, sy-vline,() spfli-cityfrom,sy-vline,()
spfli-airpfrom,sy-vline.
ULINE AT /().
ENDSELECT.
12、输出热点
**输出热点
start-of-selection.
write '请按'.
format hotspot on color inverse on.
write '热点'.
format hotspot off color off.
at line-selection.
write 'welcome!'.
13、为列表定义工具条和菜单
*为列表定义工具条和菜单
START-OF-SELECTION.
* 设置已使用的状态条
SET PF-STATUS 'STA1'.
WRITE:'测试工具条按钮 '. AT USER-COMMAND.
*当单击按钮时输出
CASE sy-ucomm.
WHEN '&NFO'.
WRITE: '已选择!'.
WHEN 'E'.
leave to screen .
when others.
WRITE ''.
ENDCASE.
14、在弹出式窗口中显示列表
*在弹出式窗口中显示列表
START-OF-SELECTION.
* 设置已使用的状态条
SET PF-STATUS 'STA1'.
WRITE:'测试工具条按钮 '. AT USER-COMMAND.
*当单击按钮时输出
CASE sy-ucomm.
WHEN '&NFO'.
WRITE: '已选择!'.
WHEN 'E'.
leave to screen .
when others.
IF sy-lsind = .
SET PF-STATUS 'STA1'.
*定义子窗口并输出
WINDOW STARTING AT ENDING AT .
WRITE '在子窗口输出!'.
WRITE: '请注意!'.
ENDIF.
ENDCASE.
15、隐藏字段技术
**隐藏字段技术
*数据库表spfli相关
TABLES: spfli, sflight. START-OF-SELECTION.
SKIP.
*输出
ULINE AT /(). WRITE: / sy-vline,() '航线承运人', sy-vline , () '航班连接', sy-vline,
() '国家代码', sy-vline,() '起飞城市', sy-vline,
() '起飞机场',sy-vline.
ULINE AT /(). *输出
SELECT * FROM spfli
* WHERE carrid = 'UA' AND connid = 3504
.
WRITE: / sy-vline,() spfli-carrid,sy-vline,
() spfli-connid,sy-vline,
() spfli-countryto,sy-vline,
() spfli-cityfrom,sy-vline,
() spfli-airpfrom, sy-vline.
*隐藏关键字
HIDE: spfli-carrid,spfli-connid.
ULINE AT /().
ENDSELECT. AT LINE-SELECTION.
*只输出第一页
IF sy-lsind = .
write : / 'spfli-carrid:',spfli-carrid.
write : / 'spfli-connid:',spfli-connid.
ULINE AT /().
WRITE: / sy-vline,() '航线承运人', sy-vline , () '航班连接', sy-vline,
() '国家代码', sy-vline,() '起飞城市', sy-vline,
() '起飞机场',sy-vline.
ULINE AT /().
*根据隐藏字段读取并打印从表
SELECT * FROM sflight
WHERE carrid = spfli-carrid AND connid = spfli-connid
.
WRITE: / sy-vline,() spfli-carrid,sy-vline,
() spfli-connid,sy-vline,
() spfli-countryto,sy-vline,
() spfli-cityfrom,sy-vline,
() spfli-airpfrom, sy-vline.
ULINE AT /().
ENDSELECT.
ENDIF.
16、使用hide技术从列表读取行
**使用hide技术从列表读取行
*数据库表spfli相关
TABLES: spfli.
DATA: sela() TYPE c,
num TYPE i VALUE . START-OF-SELECTION.
SKIP.
*输出
ULINE AT /(). WRITE: / sy-vline,'',sy-vline,() '航线承运人', sy-vline , ()
'航班连接', sy-vline,() '国家代码', sy-vline,()
'起飞城市', sy-vline,() '起飞机场',sy-vline.
ULINE AT /(). *输出
SELECT * FROM spfli.
WRITE: / sy-vline,sela AS CHECKBOX,sy-vline,() spfli-carrid,
sy-vline,() spfli-connid,sy-vline,() spfli-countryto,
sy-vline,() spfli-cityfrom,sy-vline,
() spfli-airpfrom,sy-vline.
* 隐藏关键字
HIDE: spfli-carrid,spfli-connid,spfli-countryto,
spfli-cityfrom,spfli-airpfrom.
ULINE AT /().
* 隐藏选择框,使表格行不会选择
HIDE sela.
num = num + .
ENDSELECT. END-OF-SELECTION.
* num1 = sy-lsind - 1. AT LINE-SELECTION.
WRITE : 'sy-lsind:',sy-lsind.
WRITE : 'num:',num.
*只输出第一页
IF sy-lsind = .
DO num TIMES.
* 读取行
READ LINE sy-index FIELD VALUE sela.
* 如果checked,输出隐含字段
IF sela = 'X'.
WRITE: / spfli-carrid,spfli-connid,spfli-countryto,
spfli-cityfrom,spfli-airpfrom.
ENDIF.
ENDDO.
ENDIF.
ABAP:从例子学习ABAP的更多相关文章
- 【ABAP系列】SAP ABAP中ALV使用HTML的例子
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP中ALV使用HT ...
- 【ABAP系列】SAP ABAP模块-memory内存数据传输的例子
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-memor ...
- 【ABAP系列】SAP ABAP 资产类BAPI过账 BAPI_ACC_DOCUMENT_POST
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 资产类BAPI ...
- 【ABAP CDS系列】ABAP CDS中的系统信息
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP CDS系列]ABAP CDS中的系统 ...
- 【ABAP系列】SAP ABAP 高级业务应用程序编程(ABAP)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 高级业务应用程 ...
- 【ABAP系列】SAP ABAP 带有参数的AMDP的创建
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 带有参数的AM ...
- 【ABAP系列】SAP ABAP 行列转换的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 行列转换的方法 ...
- 【ABAP系列】SAP ABAP MRKO增强
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP MRKO增强 ...
- 【ABAP系列】SAP ABAP常用正则表达式大全
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP常用正则表达式大 ...
随机推荐
- 1.Windows入侵排查思路
0x00 前言 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事故过程,同时给出解决方 ...
- Thinkphp的import使用方法
Thinkphp的import使用方法主要有以下4种,在此记下以供查询.原文链接:http://www.jb51.net/article/51765.htm 感谢. 1.用法一 import( ...
- Java静态导入
Java静态导入 静态导入的语法是: import static ...; 静态导入的好处就是可以简化一些操作,例如System.out.println(…);就可以将其写入一个静态方法 import ...
- Java中Method.invoke方法,反射?
正常来说,我们调用对象的方法是通过dot运算符来进行的,这里我们介绍另一种方法,有以下几个步骤:1,获取该类的Class Type:2,通过getMethod方法获取Method对象:3,通过调用in ...
- [WIP]php 基本语法
创建: 2019/06/14 https://www.php.net/manual/zh/langref.php php标记 当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?ph ...
- const define区别
可以使用defined()----检测常量是否设置 [问]在php中定义常量时,const与define的区别? [答]使用const使得代码简单易读,const本身就是一个语言结构,而define是 ...
- wampserver切换php版本问题
安装的wampserver有两个php版本,一个5.6的.一个7.1的,之前一直使用的php5.6的版本,今天切换7.1版本,切换成功了 phpinfo显示的版本也是7.1,但是php -v显示的却始 ...
- spring boot war
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- Jmeter3.2源码编译环境搭建(转)
1.下载jmeter3.2源码 https://github.com/apache/jmeter/tree/v3_2 https://blog.csdn.net/fly_to_higher/artic ...
- Luogu P2624 [HNOI2008]明明的烦恼 Prufer+组合+高精
好的我把标准版过了... 设$ r_i$为$i$的度数 首先,我们设 $ sum = \Sigma r_i-1$,$ tot $ 为所有能够确定度数的点 所以我们有 $ C ^ {sum} _{n-2 ...