select result from source where condition group by fileds having condition order by fields into target
select single cols .. where 当存在多条符合条件的行项目时, single随机返回其中之一
内表-当取得多条数据时, select结果会保存到内部定义的表中, 在abap里称为内表, internal table, ABAP内存中生成存储数据的虚拟表
当into语句的结果不是保存到内表时, 而是字段或者结构体时(work area),最后要加上endselect语句, 相当于在loop循环执行select语句,每读取一条,追加到结构体中,直到结束
select distinct cols … where …
endselect.
使用结构体保存查询数据
data: gt_itab type standard table of sflight, "定义gt_itab类型与标准表sflight一致
gs_wa type sflight. "定义gs_wa变量, 数据保存到这里
select * into gs_wa from sflight where carrid eq 'AA'. "把查询结果全部保存到结构体中
write: / gs_wa-carrid, gs_wa-connid.
endselect.
使用内表保存所有值
data: gt_itab type standard table of sflight, "定义内表,数据保存到这里
gs_wa type sflight.
select * into table gt_itab from sflight where carrid eq 'AA'. "指定into到内表中,要加上关键字table
loop at gt_itab into gs_wa.
write: / gs_wa-carrid, gs_wa-connid.
endloop.
动态select语句
data: gt_itab type standard table of sflight, "定义内表gt_itab
gs_wa like line of gt_itab. "定义另一个变量,引用gt_itab内表的行,这不也是一张表么?
data: gs_line() type c, "定义c类型变量,长度72
gt_list like table of gs_line(). "定义gt_list,引用gs_line,这不也是c类型么?
gs_line = 'CARRID CONNID'. "将要查询的字段放到变量中
select distinct (gs_line) into table gt_itab from sflight. "动态select条件要用括号括起来
if sy-subrc eq .
loop at gt_itab into gs_wa.
write: / gs_wa-carrid, gs_wa-connid.
endloop.
endif.
结构体
当查询一条数据时使用结构体,使用星号能取得所有字段的值,此时使用corresponding fileds of语句会自动查找到相同字段名匹配赋值
select … into | corresponding fields of ..
appending 与into
内表使用appending,给内表追加数据, into是删除内表数据之后再插入数据
data: gs_wa type sflight,
gt_itab type table of sflight.
select carrid connid
from spfli
into corresponding fields of table gt_itab
package size .
loop at gt_itab into gs_wa.
write: / gs_wa-carrid,gs_wa-connid.
endloop.
endselect.
选择动态表
parameters p_tname type char10. "定义gui上显示的输入框
data gs_wa type sflight.
select single * into gs_wa from (p_tname) where carrid = 'AA'. "点击执行按钮时,动态使用p_tname,赋值用户输入的sflight
join
默认是inner join
查询航班名字
types: begin of t_str,
carrid type sflight-carrid,
carrname type scarr-carrname,
end of t_str.
data: gs_str type t_str.
select single a~carrid b~carrname
into corresponding fields of gs_str
from sflight as a
inner join scarr as b
on a~carrid eq b~carrid
where a~carrid = 'AA'.
write: gs_str-carrid, gs_str-carrname.
动态条件
就是给where字句传值
FOR ALL ENTRIES
类似嵌套select
data gt_spfli type table of spfli.
data gt_sflight type table of sflight.
data gs_sflight type sflight.
* table of tab 内表类型/ standar table of tab 标准表类型,也可以是其他sort表.堆表 / type tab仅仅是类型
select * from spfli into table gt_spfli. "读取全部数据存到内表中,航班时刻表
select * from sflight into table gt_sflight for all entries in gt_spfli " select … for all enties in
where carrid = gt_spfli-carrid and connid = gt_spfli-connid.
loop at gt_sflight into gs_sflight.
write: / gs_sflight-carrid, gs_sflight-connid.
endloop.
聚合函数:AVG/COUNT/MAX/MIN/STDDEV/SUM
取每个航班ID平均预约占用率
data: gv_carrid type sflight-carrid,
gv_connid type sflight-connid,
gv_paymentsum type i.
select carrid connid avg( paymentsum ) into (gv_carrid,gv_connid,gv_paymentsum) from sflight group by carrid connid.
* 选择字段居然没有逗号隔开, 结构体变量,居然可以存储多个值, 什么时候用结构体变量,什么时候用内表?
write: / gv_carrid, gv_connid, gv_paymentsum.
endselect.
排序语句order by
order by primary key
限制:
order by 字段 ascending,descending,默认升序
data: gv_carrid type sflight-carrid,
gv_connid type sflight-connid,
gv_paymentsum type i.
select carrid connid avg( paymentsum ) as paymentsum into (gv_carrid,gv_connid,gv_paymentsum) from sflight group by carrid connid
order by paymentsum. "聚合函数无法作为关键字进行排序,需要定义别名
write: / gv_carrid, gv_connid, gv_paymentsum.
endselect.
Subquery,子查询,在select语句中嵌套select语句时使用
Scalar Subquery
data:gv_carrid type sflight-carrid,
gv_connid type sflight-connid,
gv_paymentsum type sflight-paymentsum.
select single carrid connid paymentsum
into (gv_carrid, gv_connid, gv_paymentsum) "真坑,左括号后不能有空格,右括号前不能有空格
from sflight as a
where carrid in ( select carrid
from spfli
where carrid = a~carrid and
connid = a~connid ) "内层嵌套select括号要留空格
and a~carrid = 'AA'.
write: gv_carrid,gv_connid, gv_paymentsum.
Non-scalar Subquery
子查询存在数据时返回TRUE,不存在返回FALSE,使用exists实现
data:gv_carrid type sflight-carrid,
gv_connid type sflight-connid,
gv_paymentsum type sflight-paymentsum.
select single carrid connid paymentsum
into (gv_carrid, gv_connid, gv_paymentsum) "真坑,左括号后不能有空格,右括号前不能有空格
from sflight as a
where exists ( select *
from spfli
where carrid = a~carrid and
connid = a~connid ) "内层嵌套select括号要留空格
and a~carrid = 'AA'.
write: gv_carrid,gv_connid, gv_paymentsum.
insert into
insert
使用insert
往scarr插入air china数据
data: gs_scarr type scarr.
gs_scarr-carrid = 'CN'.
gs_scarr-carrname = 'Air China'.
gs_scarr-currcode = 'RMB'.
gs_scarr-url = 'http://www.airchina.cn'.
insert into scarr values gs_scarr."类似python字典 values后相当于字典名
insert
[ACCEPTING DUPLICATE KEYS] 主键相同会报错,加上这句允许重复主键
data: gt_spfli type table of spfli,
gs_spfli type spfli.
gs_spfli-carrid = 'CN'.
gs_spfli-connid = ''.
gs_spfli-cityfrom = 'beijing'.
gs_spfli-cityto = 'Busan'.
append gs_spfli to gt_spfli. "1.将值存在变量,变量追加内表内容
gs_spfli-carrid = 'CN'.
gs_spfli-connid = ''.
gs_spfli-cityfrom = 'beijing'.
gs_spfli-cityto = 'Busan'.
append gs_spfli to gt_spfli.
insert spfli from table gt_spfli accepting duplicate keys. "2.将内表插入物理表
update into
update
update
update
一次性将内表的多条数据反映到数据库表中
update
修改指定字段, 比直接指定工作区的方法性能高
update
data gs_spfli type spfli.
move 'CN' to gs_spfli-carrid.
move '' to gs_spfli-connid.
move 'Shanghai' to gs_spfli-cityfrom.
gs_spfli-cityto = 'guangzhou'.
update spfli from gs_spfli. "内表反映更新数据表
update spfli
set cityto = 'beging'
where carrid = 'CN'
and connid = ''.
if sy-subrc = .
write 'update is successful'."set字段更新到数据表
endif.
delete
delete
delete from
data gs_spfli type spfli.
move 'CN' to gs_spfli-carrid.
move '' to gs_spfli-connid.
delete spfli from gs_spfli. "删除一条数据
delete from spfli where carrid = 'CN'. "删除符合where子句的数据
if sy-subrc = .
write 'delete successful'.
endif.
modify
modify
modify
data:
gt_spfli type table of spfli, "定义内表
gs_spfli type spfli. "定义表数据类型
gs_spfli-carrid = 'CN'.
gs_spfli-connid = ''.
gs_spfli-cityfrom = 'beijing'.
modify spfli from gs_spfli. "从普通数据类型修改数据到表
gs_spfli-carrid = 'CN'.
gs_spfli-connid = ''.
gs_spfli-cityfrom = 'shanghai'.
append gs_spfli to gt_spfli. "追加数据到内表 主键是carrid connid,这条数据会覆盖上面那一条
gs_spfli-carrid = 'CN'.
gs_spfli-connid = ''.
gs_spfli-cityfrom = 'shanghai'.
append gs_spfli to gt_spfli. "继续追加数据到内表 表中没有这条数据,直接插入到表
modify spfli from table gt_spfli.
parameters: p_sql() type c.
data: go_sql type ref to cl_sql_statement. "cl_sql_statement是一个类
data: gv_sql type string.
data: gd_ret type i.
create object go_sql. "这是实例化一个对象?
gv_sql = p_sql. "GUI屏幕输入实参
translate gv_sql to upper case. "将GUI屏幕输入内容转换成大写
call method go_sql->execute_update "execute_update 是一个类方法,对象调用类方法->? 这个符号前后不能有空格
exporting
statement = gv_sql "给类方法传值?输入内容?值传给谁?
receiving
rows_processed = gd_ret."输出结果?到哪里去?
输入: UPDATE SFLIGHT SET CONNID = '10' WHERE CARRID = 'AA'
手机扫一扫
移动阅读更方便
你可能感兴趣的文章