ABAP基础3:OPENSQL
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.
from
- client specified 解除自动client设置
- bypassing buffer 绕过buffer,进行物理读
- up to n rows 限制查询条目数,不就是limits么?
选择动态表
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 <tab> where <cond>
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
限制:
- 只能根据表的key值排序
- 只适用select *语句
- 在join语句和视图中无法适用
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
- 返回一列值中的一个字段值(或者聚合函数执行结果值),类似join
- 只能返回一个字段值
- 以嵌套循环方式执行
- 执行的次数是row行数目
- 提升效率,当查询重复的编号及主数据表时使用
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
- 插入一条数据
insert into <target> values <wa>
insert <target> from <wa>
使用insert <dbtab>语句,需要先定义结构体 tables: <dbtab>
往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 <target> from table <itab>
[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
update into <taget> <lines>.
update<dbtab> [client specified] <lines>
- 修改一条数据
update <target> from <wa>.
update <dbtab> 要先定义tables: <dbtab> 结构体
- 修改多条数据
一次性将内表的多条数据反映到数据库表中
update <target> from table <itab>
修改指定字段, 比直接指定工作区的方法性能高
update <target> set <set1> <set2> where <cond> set1字段1,set2字段2
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 <target> from <wa>.
delete <dbtab> 要先定义tables: <dbtab>
- 删除多条数据
delete from <target> where <cond>.
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
- 以主键为基准更新数据
- 存在数据,则用update更新
- 不存在数据,则用insert插入
- 插入一条数据
modify <target> from <wa>.
modify <dbtab> 要先定义数据类型tables: <dbtab>
- 插入多条数据,一次性修改或者追加内表所有数据
modify <target> from table <itab>
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.
- 在GUI上执行SQL命令
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'
ABAP基础3:OPENSQL的更多相关文章
- 【ABAP系列】SAP ABAP基础-abap数据类型的解析整理
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-abap数 ...
- 【ABAP系列】SAP ABAP基础-录制BDC的MODE定义解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-录制BDC ...
- 【ABAP系列】SAP ABAP基础-数据更新至数据库操作解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-数据更新至 ...
- 【ABAP系列】SAP ABAP基础-程序优化及响应速度之LOOP
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-程序优化及 ...
- 【ABAP系列】SAP ABAP基础-SQL的嵌套使用
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-SQL的嵌 ...
- ABAP基础语法
1.数据类型及属性 类型 说明 C N 0到9之间字符组成的数字字符串 D 日期格式必须为 YYYYMMDD T 格式为 24-hour的 HHMMSS I -2.147.483.648 to +2. ...
- ABAP基础篇1 内表
内表类型 abap 内表类型有三种: 标准表(一般ABAP程序中用的最多就是这种表) 系统为该表的每一行数据生成一个逻辑索引,自己内部维护着行号(Index)的编码.表的键值不唯一,且没有按照表键自动 ...
- ABAP基础篇2 数据类型
基本数据类型列表: 1.长度可变的内置类型(String.XString)1)string类型 在ABAP程序中,string类型是长度无限的字符型字段,可以和CHAR ,D,T ,I,N (F和P未 ...
- ABAP基础4:模块化
子程序定义 以form开始,以endform结束,用perform语句调用,from语句可以在程序内部/外部,perform一定要写在前面 perform. from. 子程序模块 endform. ...
随机推荐
- 2019-2020-1 20199326《Linux内核原理与分析》第一周作业
开篇概述 我利用假期的时间自学了实验楼上的Linux基础入门前八个实验的课程,学习过程中遇到了一些小问题.但经过查资料等方式最终还是解决了问题.现将学到的一些知识点总结下来.方便日后复习查看. 1.零 ...
- python学习13类2之封装
'''''''''面向对象三大特性:封装,继承,多态1.封装: 类中以_或者__的属性,都是私有属性,禁止外部调用.'''class Student(object): def __init__(sel ...
- Linux 文件常用权限
-rw------- (600) 只有所有者才有读和写的权限 -rw-r--r-- (644) 只有所有者才有读和写的权限,组群和其他人只有读的权限 -rwx------ (700) 只有所有者才有读 ...
- linux下报错:error while loading shared libraries
linux执行bin程序报: error while loading shared libraries:libncurses.so.5: cannot open shared object file: ...
- PPT模板素材
http://588ku.com/sucai/0-dnum-0-54-0-1/
- Kudu,支持快速分析的新型Hadoop存储系统
Kudu是Cloudera开源的新型列式存储系统,是Apache Hadoop生态圈的新成员之一(incubating),专门为了对快速变化的数据进行快速的分析,填补了以往Hadoop存储层的空缺.本 ...
- 解决vue渲染时闪烁{{}}的问题
原文转自: 点我 Vue页面加载时v-show设置的隐藏元素出现导致页面闪烁问题在写APP社区页面的时候在一些地方用了v-show,在刷新页面的时候就发现即便在逻辑判断为false某些元素不该显示时也 ...
- swipe 滑动操作
1.swipe() 滑动用法 swipe(self, start_x, start_y, end_x, end_y, duration=None) :Args: - start_x - 开始滑动的x坐 ...
- andorid jar/库源码解析之Bolts
目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...
- Unity 游戏框架搭建 2019 (四十四、四十五) 关于知识库的小结&独立的方法和独立的类
在上一篇,我们完成了一个定时功能,并且接触了 Action 和委托.lambda 表达式这些概念. 到目前为止,我们的库作为知识收录这个功能来说,已经非常好用了,由于使用了 partial 关键字,所 ...