内表可以用来临时存放多条数据,Open SQL允许以内表数据作为查询条件,相关语法如下:
SELECT <f1...fn> FROM <dbtab> FOR ALL ENTRIED IN <itab> WHERE....。
[FOR EXAMPLE]
TABLES:SPFLI,SFLIGHT.
DATA:BEGIN OF xpfli OCCURS 0,
carrid LIKE SPFLI-CARRID,
connid LIKE SPFLI-CONNID,
END OF xpfli.
DATA:xxflight LIKE TABLE OF xpfli WITH HEADER LINE.
SELECT CARRID CONNID INTO CORRESPONDING FIELDS OF TABLE xpfli FROM SPFLI WHERE CARRID = 'LH'.
IF NOT xpfli[] IS INITIAL. “检查是否为空
SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE xxflight FOR ALL ENTRIES IN xpfli WHERE CARRID = xpfli-CARRID AND CONNID = xpfli-CONNID.
ENDIF.
LOOP AT xxflight.
WRITE: / 'CARRID=',xxflight-carrid,
/ 'CONNID=',xxflight-connid.
ENDLOOP.
*以上执行结果如下:
需要注意的是,在Open SQL中使用 FOR ALL ENTRIED IN itab 前,一定要检查itab表是否为空,否则会造成SQL的执行效率极低。
查询表字段名转换.
在查询过程中我们通常会遇到这样的情况,将透明表查询值直接传递给内表,两表中字段结构一样,但字段名称却不同。通常的情况下将字段名称设为一致的。除此之那还可以用AS关键字,就像SQL中的用法一样。
[FOR EXAMPLE]
TABLES:SPFLI.
DATA:BEGIN OF xpfli OCCURS 0,
carrid LIKE SPFLI-CARRID,
connid2 LIKE SPFLI-CONNID,
END OF xpfli.
SELECT CARRID CONNID AS CONNID2 "将字段用AS关键字重命名
INTO CORRESPONDING FIELDS OF TABLE xpfli FROM SPFLI WHERE CARRID = 'LH'.
LOOP AT xpfli.
WRITE: / 'CARRID=',xpfli-carrid,
/ 'CONNID2=',xpfli-connid2.
ENDLOOP.
*执行结果如下截图: