ABAP之SQL操作(select、insert、update、delete、modify)⼀、SELECT 语句
SELECT <lines>[DISTINCT]<columns>[AS<alias>]
INTO|APPEND [CORRESPONDING FIELDS OF]<wa> TABLE<itab>[PACKAGE SIZE<n>]..
FROM <dbtab>[AS <alias>]<options> UP TO <n>
[INNER]JOIN <dbtab> [AS <alias>] ON <cond><options>...
WHERE <s> <opertor> <f> ...
GROUP BY <f1> <f2>..
HIVING <condition>.
ORDER BY PRIMARY KEY.
lines:single
读取⼀条数据:.
SELECT SINGLE <cols> ... WHERE
注意:限制好where条件,否则返回任意⼀条数据,与预期结果不同.
DISTINCT :删除重复值。
SELECT [DISTINCT]<cols>...WHERE
当INTO 为字段或者结构体时,结尾必须加上ENDSELECT;如果是内表则不需要。
AS<alias>:别名
SELECT <cols> [AS <cols>]..
INTO语句:
1. [CORRESPONDING FIELDS OF]<wa>:⾃动给相同字段名匹配赋值。wa为结构体,变量,查询⼀条数据时,常⽤语loop循环中。SELECT ...INTO [CORRESPONDING FIELDS OF]<wa> .
2.内表
查询多条数据是使⽤内表,须使⽤APPENDING,不能使⽤INTO,INTO是删除内表数据后插⼊时使⽤的。
SELECT ...INTO|APPEND [CORRESPONDING FIELDS OF]<wa> TABLE<itab>[PACKAGE SIZE<n>]..
PACKAGE SIZE:表⽰每次追加到内表的条数,此时须使⽤ENDSELECT.
SELECT INTO CORRESPONDING FIELDS OF TABLE itab
PACKAGE SIZE 5
FROM SPFLI.
ENDSELECT.
FROM语句:
1.选择静态表:
SELECT ...FROM <dbtab>[AS <alias>]<options>
2.JOIN语句
SELECT ...FROM <tab>[INNER] JOIN <dbtab> [AS <alias>] ON <cond><options>...
3.限制查询个数
SELECT ...FROM <tab> UP TO <n>
WHERE语句:
1.where条件语句
SELECT ...WHERE <s> <opertor> <f> ...
2.常⽤语句
SELECT ...WHERE <s> [NOT]BETWEEN <f1> AND <F2>.
SELECT ...WHERE COL2 LIKE'_ABC%'.
SELECT ...WHERE <s> [NOT]IN (<f1>,..,<fn>)...
SELECT ...WHERE <s> [NOT]IN <seltab>... 查询存在selection table,Range变量⾥的数据。
3.FOR ALL ENTRIES 语句.
SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond>
SELECT MATNR MTART
FROM MARA
INTO CORRESPONDING FIELDS OF TABLE GT_MARA
WHERE MATNR IN S_MATNR.
IF GT_MARA[] IS NOT INITIAL.
SELECT MATNR MAKTX
FROM MAKR
INTO CORRESPONDING FIELDS OF TABLE GT_MAKT
FOR ALL ENTRIES IN GT_MARA
WHERE MATNR IN S_MATNR
AND SPRAS = SY-LANGU
AND MATNR = GT_MARA-MATNR.
ENDIF.
SORT GT_MAKT BY MATNR.
LOOP AT GT_MARA.
CLEAR GT_TOTAL.
MOVE-CORRESPONDING GT_MARA TO GT_TOTAL.
READ TABLE GT_MAKT WITH KEY MATNR = GT_MARA-MATNR
BINARY SEARCH.
IF SY-SUBRC = 0.
GT_TOTAL-MAKTX =GT_MAKT-MAKTX.
ELSE.
CONTINUE.
ENDIF.
APPEND GT_TOTAL.
ENDLOOP.
View Code
注意:
1.itab的字段要与⽐较对象的表字段类型⼀致。
2.判断内表itab不能为空,否则相当于全表扫描。
3.程序中的continue退出当前循环,执⾏下⼀次。本语句执⾏时,相当于INNER-JOIN;不执⾏时,相当于LEFT-JOIN.
4.不能与sum、avg、max、min等聚合函数⼀起使⽤,group by。
5.使⽤该选项后,对于最后得出的结果集系统会⾃动删除重复⾏。因此如果要保留重复⾏记录时,记得在SELECT语句中添加⾜够字段GROUP :
SELECT <f1> <f2> <agg>...
GROUP BY <f1> <f2> ...
HIVING <condition>.
ORDER BY:
delete in-根据标的key值进⾏排序。
-只适合于SELECT * 语句。
-在JOIN语句和视图中⽆法使⽤。
SELECT ...
ORDER BY <f1> [ASCENDING|DESCENDING]
<f2> [ASCENDING|DESCENDING]
⼆、INSERT
⼀条数据
INSERT INTO <target> VALUES <wa>.
INSERT <target> FROM <wa>.
多条数据.
INSERT <target> FROM TABLE <itab> [ACCEPTING DUPLICATE KEYS]
插⼊数据时,避免有相同主键引起dump error错误,使⽤ACCEPTING DUPLICATE。此时sy-subrc返回4.
三、UPDATE
⼀条数据
UPDATE <target> FROM <wa>. "表结构相同
多条数据.
UPDATE <target> FROM TABLE <itab>.
UPDATE <target> SET <set1> <set2> WHERE <cond>.
四、DELETE
⼀条数据
DELETE <target> FROM <wa>. "表结构相同
多条数据.
DELETE FROM <target> WHERE <cond>.
五、MODIFY.
⼀条数据
MODIFY <target> FROM <wa>. "表结构相同
多条数据.
MODIFY <target> FROM TABLE <itab>.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论