OPEN SQL
1. SELECT 语法结构
SELECT <result> FROM <dbtab>
INTO <target>
WHERE <condition>
GROUP BY <fields> ORDER BY <fields>
SELECT <result>:具体的查询字段。
SELECT SINGLE:定义单行查询。
FROM <dbtab>:所查询的透明表。
INTO <target>:查询结果赋值对象,赋值到具体表或结构体。
INTO (<f1>...<fn>):将查询结果赋值到具体字段。
INTO CORRESPONDING FILES OF <itab>:将查询结果按字段匹配赋值给具体的表或者结构体。
WHERE <condition>:查询条件。
GROUP BY <fileds>:分组查询条件。
ORDER BY <fields>:排序条件。
TABLES:spfli.
DATA: tab LIKE spfli.
SELECT SINGLE carrid connid countryfr cityfrom FROM spfli
INTO CORRESPONDING FIELDS OF tab.
WRITE:/ 'TAB-CARRID=',tab-carrid,
/ 'TAB-CONNID=',tab-connid,
/ 'TAB-COUNTRYFR=',tab-countryfr,
/ 'TAB-CITYFROM=',tab-cityfrom.
使用SELECT...ENDSELECT语法能循环读取及操作查询,通过系统参数SY-DBCNT可以获取当前读取数据的行数,该语法中不能加入SINGLE、INTO CORRESPONDING FIELDS、TABLE等关键字,中间可以使用CHECK语法来判断查询值,如下:
TABLES:sbook.
DATA:counts TYPE i,total TYPE p DECIMALS 2,average TYPE f.
DATA:connid LIKE sbook-connid.
*select single luggweight from sbook into total.
*write total.
SELECT connid COUNT(*) SUM( luggweight ) AVG( luggweight )
INTO (connid,counts,total,average) FROM sbook
*where carrid = 'LH' AND fldate = '19950228'
GROUP BY connid.
WRITE: / 'SY-DBCNT=',sy-dbcnt,
/ 'CONNID=', connid,
/ 'COUNTS=',counts,
/ 'TOTAL=',total,
/ 'AVERAGE=',average.
CHECK total > 10.
WRITE: / 'THW SUM WEIGHT MORE THAN 10.'.
ENDSELECT.
2. SELECT 常用标准函数
Open SQL 常用的标准函数如下:
COUNT():统计查询总数。
SUM():统计表中某个数值字段的总和。
AVG():统计表中某个数值字段的平均值。
MAX():统计表中某个字段的最大值。
MIN() :统计表中某个字段的最小值。
3. 多个表数据联合查询
当需要同时查询多个表的相关字段时,需要使用到JOIN语句,共包含三种:
INNER JOIN:查询结果包含两个连接表中彼此相对应的数据记录。
LEFT OUTER JOIN:查询结果集中包含左则表中的所有数据记录,右表中仅查询出包含相对应的匹配条件的数据。
FULL OUTER JOIN:包含左右表所有的记录。
TABLES:spfli,sflight.
DATA:BEGIN OF xflight OCCURS 0,
airpfrom LIKE spfli-airpfrom,
airpto LIKE spfli-airpto,
fldate LIKE sflight-fldate,
deptime LIKE spfli-deptime,
END OF xflight.
SELECT a~airpfrom a~airpto a~deptime b~fldate
INTO CORRESPONDING FIELDS OF TABLE xflight FROM spfli AS a
INNER JOIN sflight AS b
ON a~carrid = b~carrid AND a~connid = b~connid
ORDER BY a~airpfrom a~airpto b~fldate a~deptime.
LOOP AT xflight.
WRITE:/ 'Airpfrom= ',xflight-airpfrom,
/ 'Airpto=', xflight-airpto.
ENDLOOP.
4. 參照内表条件进行查询
内表可以用来临时存放多条数据,OPEN SQL允许以内表数据作为查询条件,語法如下:
SELECT <f1...fn> FROM <dbtab> FOR ALL ENTRIED IN <itab> WHERE...
TABLES:spfli,sflight.
DATA:BEGIN OF xpfli OCCURS 0,
carrid LIKE spfli-carrid,
connid LIKE spfli-connid,
END OF xpfli.
DATA:xflight LIKE TABLE OF xpfli WITH HEADER LINE.
SELECT carrid connid INTO CORRESPONDING FIELDS OF TABLE xpfli FROM spfli
WHERE carrid = 'DL'.
IF NOT xpfli[] IS INITIAL."检查是否为空
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE xflight
FOR ALL ENTRIES IN xpfli
WHERE carrid = xpfli-carrid AND connid = xpfli-connid.
ENDIF.
LOOP AT xflight.
WRITE: / 'CARRID=',xflight-carrid,
/ 'CONNID=',xflight-connid.
ENDLOOP.
在OPEN SQL中使用FOR ALL ENTRIED IN itab 前,一定要检查itab表是否为空,否则会造成SQL的执行效率极低。
5. 查询表字段名转换。
在查询过程中我们通常会遇到这样的情况,将透明表查询值直接传递给内表,两表中字段结构一样,但字段名称却不同。通常解决方法是将内表字段按透明表字段名创建,另一种方法,我们可以在SELECT查询时将其直接转换。
TABLES:spfli.
DATA:BEGIN OF xpfli OCCURS 0,
carrid LIKE spfli-carrid,
connid2 LIKE spfli-connid,
END OF xpfli.
SELECT carrid connid AS connid2"直接将查询的字段重新命名
INTO CORRESPONDING FIELDS OF TABLE xpfli
FROM spfli where carrid ='SQ'.
LOOP AT xpfli.
WRITE:/ 'carrid=',xpfli-carrid,
/ 'connid2=',xpfli-connid2.
ENDLOOP.
6. 限制OPEN SQL获取数据条数
在OPEN SQL中可以使用UP TO n ROWS语法来限制读取数据的条件,使用UP TO n ROWS可读取数据指定前n条,但是不能继续读取数据,语法如下:
SELECT * INTO ... UP TO 5 ROWS. "该语句只读取所抓数据的前5条。
TABLES:spfli.
DATA:lt_spfli LIKE STANDARD TABLE OF spfli WITH HEADER LINE.
SELECT * FROM spfli
INTO CORRESPONDING FIELDS OF TABLE lt_spfli UP TO 8 ROWS.
LOOP AT lt_spfli.
WRITE:/ sy-tabix,'lt_spfli-carrid=',lt_spfli-carrid.
ENDLOOP.
使用PACKAGE SIZE n,可以连续读取数据线,每次读取指定条件,但其必须应用于SELECT...ENDSELECT语句中,例如:
TABLES:spfli.
DATA:lt_spfli LIKE STANDARD TABLE OF spfli WITH HEADER LINE.
SELECT * FROM spfli
INTO CORRESPONDING FIELDS OF TABLE lt_spfli
PACKAGE SIZE 5.
ULINE.
LOOP AT lt_spfli.
WRITE:/ 'carrid=',lt_spfli-carrid,
'connid=',lt_spfli-connid,
'airpfrom=',lt_spfli-airpfrom.
ENDLOOP.
ENDSELECT.
程序每次读取5行后输了, 如图所示: