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常用正则表达式大 ...
随机推荐
- 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)
- Thinkphp的import使用方法
Thinkphp的import使用方法主要有以下4种,在此记下以供查询.原文链接:http://www.jb51.net/article/51765.htm 感谢. 1.用法一 import( ...
- C# BackgroundWorker(异步线程)
日期:2018年11月28日 环境:Windows 10,VS2015 前言 .NET 类库中提供了一个快捷使用多线程的帮助类BackgroundWorker,能够快速创建一个新的线程,并能报告进度 ...
- Web调试利器fiddler介绍
转载:http://blog.chinaunix.net/uid-27105712-id-3738821.html 最近在使用fiddler,发现这个真是非常最犀利的web调试工具,笔者这里强烈推荐给 ...
- Deep Image Matting
论文地址:https://arxiv.org/abs/1703.03872 TF复现地址:https://github.com/Joker316701882/Deep-Image-Matting 领域 ...
- 数组API(2)
ES5中数组新增API: 1. indexOf 作用:检测当前数据是否在数组中存在: 特点:如果存在返回相对应的下标,如果不存在则返回-1: 如果数组中存在多个需要判断的这个数据,则返回最近的这个数据 ...
- 让你的IDEA倍爽
使用IDEA开发有 一段时间了,从陌生到熟悉的过程算是很平稳的度过,感谢IntelliJ IDEA交流群(群号244908708)里面的群友对我提供的帮助,感谢群主的github上面提供的资料. 这篇 ...
- idea dao使用@Mapper注解 业务类使用@Autowired 注入dao 爆红问题
实际项目跑起来无影响,但是看起来不太爽. 可以在dao类添加org.springframework.stereotype.Repository 注解 或者可以在service类中使用 javax.an ...
- POJ1033 Defragment
题目来源:http://poj.org/problem?id=1033 题目大意: 某操作系统的文件系统中,所有的磁盘空间被分为N个大小相等的cluster,编号1至N.每个文件占用一个或多个clus ...
- 前端实现Tab切换栏
tab切换,所需的 UI 只有两组元素 - Header 和 Tab,下面介绍几种不同的实现方法和他们的优缺点 本文主要说一些 CSS 的实现方法.最好的方法是 第四种 => label + i ...