本文主要介绍SAP ABAP中OPEN SQL的SELECT语句
SELECT语句简介
SELECT <lines> <columns> FROM <db> WHERE <condition>
其中<lines>代表查询的件数,<columns>代表查询的字段名
SELECT SINGLE
SELECT SINGLE <cols> FROM <db> WHERE <condition>
该语句用于从数据库表中查询单条数据
SELECT SEVERAL LINE
SELECT [DISTINCT] <columns> FROM <db> WHERE <condition>
[DISTINCT]为可选项,若选择则自动删除所查询数据的重复项,在本文最后的一个案例代码效果中有演示!!
SELECT并且输出打印的两种方式
1. SELECT...ENDSELECT
REPORT ZWYZTEST_FOR_MANGO. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. DATA:GS_SFLIGHT TYPE SFLIGHT. SELECT * INTO GS_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AC'. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID. ENDSELECT.
2.SELECT(LOOP...ENDLOOP)
REPORT ZWYZTEST_FOR_MANGO. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. DATA:GS_SFLIGHT TYPE SFLIGHT. SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AC'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID. ENDLOOP.
数据库表SFLIGHT(最下方案例也使用该数据库表):
SELECT...AS
SELECT <columns> AS <alias>...
AS可以用来为所查询的数据库表取别名,<alias>为表的别名
动态SELECT语句
可以动态定义SELECT语句的字段,保存动态语句的结构体GS_LINE最多可以容纳72位CHAR类型,结构体GS_LINE为NULL时与*相同
1.使用DISTINCT
REPORT ZWYZTEST_FOR_MANGO. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. DATA:GS_SFLIGHT TYPE SFLIGHT. DATA:GS_LINE(72) TYPE C. GS_LINE = 'CARRID CONNID'. SELECT DISTINCT (GS_LINE) INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AC'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID. ENDLOOP.
2. 不使用DISTINCT
REPORT ZWYZTEST_FOR_MANGO. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. DATA:GS_SFLIGHT TYPE SFLIGHT. DATA:GS_LINE(72) TYPE C. GS_LINE = 'CARRID CONNID'. SELECT (GS_LINE) INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AC'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID. ENDLOOP.