SAP ABAP——内表(十)【读取内表数据】

简介: 本文主要介绍一下SAP ABAP中内表的增删查改语句中的读取内表数据的相关语句,包括利用关键字读取数据,利用索引读取数据

本文主要介绍一下SAP ABAP中内表的增删查改语句中的读取内表数据的相关语句,包括利用关键字读取数据,利用索引读取数据


利用关键字读取数据

READ TABLE itab FROM wa1 INTO wa2.
READ TABLE itab WITH TABLE KEY k1 = f1 k2 = f2 ... kn = fn INTO wa.

案例代码演示(不使用TABLE KEY)

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID,
      GS_SFLIGHT  TYPE SFLIGHT,
      GS_SFLIGHT1 TYPE SFLIGHT,
      GS_SFLIGHT2 TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据展示如下:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
ULINE.
GS_SFLIGHT1-CARRID = 'BC'.
GS_SFLIGHT1-CONNID = 0820.
GS_SFLIGHT1-FLDATE = '20200718'.
GS_SFLIGHT1-PRICE = 1700.
READ TABLE GT_SFLIGHT FROM GS_SFLIGHT1 INTO  GS_SFLIGHT2. "读取了上述工作区中数据
WRITE:'读取数据如下:'.
WRITE:/ GS_SFLIGHT2-CARRID,GS_SFLIGHT2-CONNID,GS_SFLIGHT2-FLDATE,GS_SFLIGHT2-PRICE.

效果演示(不使用TABLE KEY)

dd92f5f9ddfbb052c26150e3a529a95.png


案例代码演示(使用TABLE KEY)

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID CONNID FLDATE,
      GS_SFLIGHT  TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据展示如下:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
ULINE.
CLEAR GS_SFLIGHT.
READ TABLE GT_SFLIGHT WITH TABLE KEY CARRID = 'AC' CONNID = 0820 FLDATE = '20200717' INTO GS_SFLIGHT. "使用TABLE KEY 读取数据
WRITE:'读取数据如下:'.
WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.

效果演示(使用TABLE KEY)

2da2db0e2d8696608f9a29792ed8160.png


利用索引读取数据

READ TABLE itab INDEX n INTO wa.

案例代码演示

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID CONNID FLDATE,
      GS_SFLIGHT  TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据展示如下:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
ULINE.
CLEAR GS_SFLIGHT.
READ TABLE GT_SFLIGHT INDEX 3 INTO GS_SFLIGHT. "使用TABLE KEY 读取数据
WRITE:'读取数据如下:'.
WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.

效果演示

237bb45c8fb5c6f094869324c56846b.png

目录
相关文章
|
6月前
SAP CLIENT 数据配置文件的导出/导入
SAP CLIENT 数据配置文件的导出/导入
159 1
SAP ABAP 程序调用设置成后台作业模式
ABAP 程序调用设置成后台作业模式
523 0
|
数据处理
|
SQL
SAP ABAP-OPENSQL-数值函数简介
S4中针对OPENSQL新提供一些函数
139 0
|
SQL
SAP ABAP-OPENSQL-字符串函数介绍
S4中SQL新语法字符串函数
209 0
|
SQL
SAP ABAP-OPENSQL-日期/时间戳函数
S4提供针对OPENSQL一些新的SQL函数调用,本文展示相关函数的使用
661 0
SAP ABAP-删除工序示例代码
ABAP-删除工序示例代码
171 0
|
6月前
|
程序员
开发语言漫谈-ABAP
ABAP是SAP公司专门用于SAP软件环境的专门语言
|
SQL 设计模式 前端开发
【置顶】SAP ABAP开发实战——从入门到精通系列目录
本文章为SAP ABAP开发实战——从入门到精通系列的目录以及关于该教程的后续写作计划表
1593 0
【置顶】SAP ABAP开发实战——从入门到精通系列目录
|
BI
SAP ABAP在线预览文档对象的开发实现
应用场景:有些定制化开发(报表/功能增强等)完成之后,客户需要将其操作手册或者相关文档放在某个报表的初始画面,供实际操作者在线查阅,当然这个功能也同样类似于模板的下载,这里就以在线预览(直接打开)为例进行说明。
225 0