在数据库SQL查询中当要获取前几条数据时会用到关键字TOP,在Open SQL中也有相类似的功能,可以使用 UP TO n ROWS语法来限制读取数据的条数,语法如下:
SELECT * INTO ...... UP TO N ROWS.
[FOR EXAMPLE]
TABLES:SPFLI.
DATA: P_SPFLI LIKE STANDARD TABLE OF SPFLI WITH HEADER LINE.
SELECT * FROM SPFLI INTO CORRESPONDING FIELDS OF TABLE P_SPFLI UP TO 5 ROWS.
LOOP AT P_SPFLI.
WRITE: / SY-TABIX, ':P_SPFLI-CARRID=',P_SPFLI-CARRID.
ENDLOOP.
*执行结果如下截图:
使用PACKAGE SIZE N连续读取数据。
[FOR EXAMPLE]
TABLES:SPFLI.
DATA:T_SPFLI LIKE STANDARD TABLE OF SPFLI WITH HEADER LINE.
SELECT * FROM SPFLI
INTO CORRESPONDING FIELDS OF TABLE T_SPFLI
PACKAGE SIZE 5 UP TO 20 ROWS. ”UP TO 20 ROWS限制返回20行数据,否则返回所有的数据。
ULINE.
LOOP AT T_SPFLI.
WRITE: / SY-TABIX,':T_SPFLI-CARRID=',T_SPFLI-CARRID,
/ SY-TABIX,':T_SPFLI-CONNID=',T_SPFLI-CONNID,
/ SY-TABIX,':T_SPFLI-AIRPFROM=',T_SPFLI-AIRPFROM,
/ SY-TABIX,':T_SPFLI-AIRPTO=',T_SPFLI-AIRPTO.
ENDLOOP.
ENDSELECT.
*以上执行结果部分截图如下: