1.SQL命令
命令 |
单条/多条 |
示例 |
附加说明 |
SELECT/ SELECT SINGLE |
单条查询 |
SELECT id, name,age FROM dbtab INTO TABLE @DATA(itab). |
|
多条查询 |
SELECT SINGLE name INTO @DATA(lv_name) FROM dbtab. |
|
|
INSERT |
单条插入 |
INSERT INTO name VALUES lv_name. |
|
多条插入 |
INSERT id,name,age FROM TABLE itab [ACCEPTING DUPLICATE KEYS ]. |
ACCEPTING DUPLICATE KEYS:如果出现关键字相同的条目,SY-SUBRC = 4,并且会跳过该条目,但其余数据会插入。 |
|
UPDATE |
单条更新 |
UPDATE dbtab FROM wa. |
wa:结构与数据表一致,字段名可以不同。 |
多条更新 |
UPDATE dbtab FROM TABLE itab. / UPDATE dbtab SET id = lv_id, name = lv_name, age = lv_age WHERE (where). |
|
|
DELETE |
单条删除 |
DELETE dbtab FROM wa. |
|
多条删除 |
DELETE FROM dbtab WHERE (where). |
|
|
MODIFY
|
单条修改 |
MODIFY dbtab FROM wa. |
已存在的记录进行修改,新数据则进行插入 |
多条修改 |
MODIFY dbtab FROM TABLE itab. |
Open SQL 执行成功:SY-SUBRC = 0;执行失败:SY-SUBRC <> 0。
2.SELECT标准函数和分组过滤
标准函数 |
说明 |
COUNT() |
查询记录总数 |
SUM() |
汇总 |
AVG() |
平均值 |
MAX() |
最大值 |
MIN() |
最小值 |
分组过滤:将标准函数和GROUP BY一起使用
SELECT AGV(age) AS avg_age INTO @DATA(lv_avg_age) FROM dbtab GROUY BY class
HAVING MAX(age) > 15.
3.联合查询
联合方式 |
说明 |
INNER JOIN |
量表数据完全匹配 |
LEFT JOIN |
右表不需要完全匹配 |
RIGHT JOIN |
左表不需要完全匹配 |
4.查询条件
操作符 |
说明 |
示例 |
EQ |
= |
… WHERE name EQ ‘lili’. / … WHERE name = ‘lili’. |
NE |
<> / >< |
… WHERE id NE 5. / … WHERE id <> 5. / … WHERE id >< 5. |
LT |
< |
… WHERE id LT 5. / … WHERE id < 5. |
LE |
<= |
… WHERE id LE 5. / … WHERE id <=5. |
GT |
> |
… WHERE id GT 5. / … WHERE id > 5. |
GE |
>= |
… WHERE id GE 5. / … WHERE id >= 5. |
BETWEEN a AND b |
在a和b之间 |
… WHERE id BETWEEN 3 AND 8. |
LIKE |
“_”用于替代单个字符,“%”用于替代任意字符串,包括空字符串(用于模糊查询) |
… WHERE name LIKE ‘li_%’. |
IN |
满足IN后面列表中的任何一个 |
… WHERE name IN (‘lili’,’xx’,’nm’). |
5.FOR ALL ENTRIES IN 语句
Open SQL允许通过FOR ALL ENTRIES IN将内表数据作为查询条件,在数据量庞大的情况下,使用FOR ALL ENTRIES IN会比INNER JOIN查询速度快。
INNER JOIN |
FOR ALL ENTRIES IN |
SELECT d1~id,d1~name,d2~score FROM dbtab1 AS d1 INNER JOIN dbtab2 AS a2 ON a2~id = d1~id INTO TABLE ty_stu. |
SELECT id INTO TABLE ty_stuid FROM dbtab 2. IF ty_stuid IS NOT INITIAL. SELECT name FROM dbtab1 INTO TABLE ty_stu FOR ALL ENTRIES IN ty_stuid WHERE id = ty_stuid~id. ENDIF. |
使用注意事项:
(1) 结果集会自动删除重复行
(2) FOR ALL ENTRIES IN后跟的itab为空,则选出所有记录,SELECT无意义
(3) Itab条件不能使用BETWEEN、LIKE、IN
(4) 不能使用ORDER BY、HAVING
(5) 可以使用COUNT(),但不能使用SUM()、AVG()、MAX()、MIN()
6.动态SQL
DATA: lv_where1 TYPE string,
lv_where2 TYPE string,
lv_where TYPE string.
lv_where1 = ‘d1~id IN @p_id’.
IF F1 EQ 1.
lv_where2 = ‘AND name = @p_name’
ENDIF.
CONCATENATE lv_where1 lv_where2 INTO lv_where SEPARATED BY ‘ ‘.
SELECT id, name, age FROM dbtab WHERE ( lv_where ).
7.子查询
(1) ( =、<>、<、<=、>、>= ) / ( ALL、SOME、ANY )
SELECT * FROM dbtab1 INTO TABLE ty_stu WHERE id >= ALL ( SELECT SINGLE id FROM dbtab2 WHERE … ).
(2) IN/NOT IN、EXISTS/NOT EXISTS
SELECT * FROM dbtab1 INTO TABLE ty_stu WHERE name IN ALL ( SELECT name FROM dbtab2 WHERE … ).
8.游标
DATA: c TYPE cursor. ”声明游标
DATA: wa TYPE dbtab.
OPEN CURSOR: c FOR SELECT id name FROM dbtab WHERE class = 5. ”打开游标
DO.
FETCH NEXT CURSOR c INTO CORRESPONDING FIELDS OF wa. ”读取数据
IF sy-subrc <> 0.
CLOSE CURSOR c. ”关闭游标
EXIT.
ELSE.
WRITE: wa-id, wa-name.
ENDIF.
ENDDO.
9.UP TO N ROWS限定查询条数
SELECT * FROM dbtab INTO CORRESPONDING FIELDS OF TABLE ty_stu UP TO 10 ROWS.
10.WITH将查询出来的数据作二次查询
WITH +itab AS ( SELECT FROM dbtab1 AS d1
INNER JOIN dbtab2 AS d2
ON d2~id = d1~id
FIELDS
d1~id,
d1~name,
d1~age,
d2~score )
SELECT FROM +itab as i
LEFT JOIN dbtab3 AS d3
ON d3~id = i~id
FIELDS
i~id,
i~name,
i~age,
i~score,
d3~class
INTO TABLE @DATA(lv_stu).