ABAP中内表的操作(增删改查)

关于ABAP中的内表的创建:

-- 定义内表 --
TYPES: BEGIN OF ty_tab1,
		field1(10) TYPE c,
		field2(2) TYPE i,
	   END OF ty_tab1.
DATA: itab1 TYPE TABLE OF ty_tab1.

-- 定义工作区 --
DATA: ls_itab1 TYPE ty_tab1.

-- 往内表中添加数据 ---
ls_itab1-field1 = '小明'.
ls_itab1-field2 = 00001.

INSERT ls_itab1 INTO itab1 INDEX 1.
APPEND ls_itab1 TO itab1.
COLLECT ls_itab1 INTO itab1.

-- 往内表中添加一另一个内表 --
APPEND LINES OF itab1 TO itab2.
APPEND LINES OF itab1 FROM 1 TO 2 TO itab2.
INSERT LINES OF itab1 INTO itab2 INDEX 2.

-- 修改内表中的数据 --
ls_itab1-field1 = 'field1'.
ls_itab1-field2 = 999.

ls_itab2-field1 =  '更新'.
ls_itab2-field2 = 888.

MODIFY itab2 FROM ls_itab1 INDEX2.
MODIFY itab2 FROM ls_itab1 INDEX3 TRANSPORTING field1.
MODIFY itab2 FROM ls_itab1 INDEX3 TRANSPORTING field2 WHERE field1 = 'field1'.

-- 删除数据 --
DELETE itab2 WHERE field1 = '00001' AND field2 = '888'.

ls_itab2-field1 = '更新'.
ls_itab2-field2 = 888.
DELETE TABLE itab2 FROM ls_itab2.

DELETE itab2 INDEX 1.
DELETE ADJACENT DUPLICATES FROM itab2.
DELETE ADJACENT DUPLICATES FROM itab2 COMPARING ALL FIELDS.
DELETE ADJACENT DUPLICATES FROM itab2 COMPARING field1 field2.

-- 查询数据 --
DATA sitab TYPE TABLE OF spfli WITH HEADER LINE.
SELECT
	carrid connnid airpfrom airpto
FROM spfli
INTO CORRESPONDING FIELDS OF TABLE sitab
WHERE carrid = 'UA' AND airpto = 'VIE'.
CHECK sy-subrc = 0.

LOOP AT sitab.
	WRITE: / 'sitab-Carrid=', sitab-carrid,
			 'sitab-connid=', sitab-connid,
			 'sitab-Airpfrom=', sitab-airpfrom,
			 'sitab-Airpto=', sitab-airpto.
ENDLOOP.

-- 查询 + 标准函数 --
DATA: counts_TYPE i,
	  total TYPE p DECIMAL 2,
	  average TYPE f.
DATA: connid LIKE sbook-connid.

SELECT connid count( * ) sum( luggweight ) avg( luggweight )
into (connid, counts, total, average)
from sbook
group by connid.

check sy-subrc = 0.
write: / 'sy-dbcnt=', sy-dbcnt,
	   /'connid=', connid,
	   / 'counts=', counts,
	   / 'total=', total,
	   / 'average=', average.
check total > 10.
write: / 'The sum weight more than 10'.
ENDSELECT.

-- 表连接 --
DATA: BEGIN of xflight OCCURS 0,
		carrid2 TYPE spfli-carrid,
		airprom LIKE spfli-airpfrom,
		airpto LIKE spfli-airpto,
		fldate LIKE sflght-fldate,
		deptime LIKE spfli-deptime,
	  END OF xflight.
	  
select 
	a~carrid as carrid2 a~airpfrom a~airpto b~fldate a~deptime 
into table xflight
from spfli as a inner join sflight as b
on a~carrid = b~carrid and a~connid = b~connid.

LOOP AT xflight.
	write: / 'carrid2=', xflight-carrid2, 'Airpfrom=', xflight-airpfrom, ' ,to', xflight-airpto.
ENDLOOP.

你可能感兴趣的:(ABAP中内表的操作(增删改查))