本文主要介绍一下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)
案例代码演示(使用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)
利用索引读取数据
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.
效果演示