SAP ABAP 学习总结
ADT: ABAP Development Tools
ABAP 大小写不敏感
S/4 HANA 在数据库端处理了大部分数据,减小客户端的负载。
XString: 一般用于十六进制数据,常用于存放图片
ABAP基本数据类型:
I INT8 DECFLOAT16 DECFLOAT32
C P STRING XSTRING
N: number
D: date
T: time
数据类型:
定义单个变量:
predefined: complete: i, int, decfloat16, decfloat32, d, t, string, xstring
imcomplete: n, c, p(price), x (需要指定length, p还要指定精度)
defined: dictionary:
table-field,
structure-field,
data-element(描述:short, medium, long, header; 技术: type, length)
程序中定义的变量:
Types: t_cnt type c.
data: int_1 type t_cnt.
运用class type:
data: int2 type cl_s4d400_calculator=>ty_int (class中定义的一个变量类型)
定义多个变量,structure: type定义的几种方式
直接从dictionary中取类型: table-type, structure-type
在程序中定义: types: begin of …. end of…
从程序中取: class=>type
定义多条数据: internal table: type定义的几种方式
直接从dictionary中取类型: table-type
在程序中定义(p153):types t_table type standard table of d400_s_flight
with unique key carrid connid.
常用快捷键:
CTRL + Space 代码提示
CTRL + Shift + A 打开包查询, 打开class查询,打开table查询
CTRL + H 查询program
CTRL + SHIFT + N 新建包文件
CTRL + N 新建文件
选中方法按
F2: 方法简要说明
F3:查看方法具体代码
F1: 关键字使用方法。
CTRL+1 在implenation class中自动生成此代码框架
CTRL + SHIFT + > 取消注释
CTRL + SHIFT + < 添加注释
CTRL + F2 检查
CTRL + F3 激活
F8: 运行程序
F5: 单步执行, 进入
F6: 执行
data name.
没有具体写type和length, 默认为c类型, 长度为1(type: c 的default长度是1)
包名和program必须以z开头
Z: 说明完全是用户自由创建的
Y: 从库扩展而来的
class名 => static 方法
class 名 -> 非static 方法
clear variable: 让variable回到默认值
sy-subrc:
0: correct
4: no data found in database
8:error
程序中出现的text需要手动维护:
右击programe à open with sap gui à menu à goto à text element
注意: 需要保存并active,之后才可以使用
从debug模式回到代码编写模式:
window à perspective à open perspective à other à ABAP
自定义的变量以gv开头
定义method时,
importing定义的变量,一般以iv开头
exporting 定义的变量, 一般以ev开头
若是call方法,则相反。
exception相关的变量(raising), 以 cx开头
try.
catch cx_...
endtry.
定义一个function:
function name
importing
exporting
exceptions … (使用之前提前定义过的exception)
自定义结构体:
- 在某个文件中定义
types: begin of structure_name,
attribute1 type type1,
attribute2 type type2 ,…
end of structure_name.
- 直接定义成一个文件,(DDL)
define type 名称 {
attr1 : type1;
attr2 : type2;
}
结构体赋值:
- 单个属性赋值
structure-attr1 = value1.
structure-attr2 = value2.
- 整体赋值
structure = value #().
- 修改其中某个属性值,其他保持不变(base关键字)
structure = value #( base structure
structure-attr1 = structure-attr1 + 5).
- 利用已有的结构体赋值(可能类型不一致)
structure = corresponding #( structure2 ).
structure = corresponding #( base ( structure )
structure2 ).
ABAP内部表:
standard, sorted, hashed
内部表internal tables 赋值
- 先对行(structure)赋值,再对表赋值
利用上述方法对每行structure赋值
table = value #( (structure ) )
- 直接对表内的每行属性赋值
table = value #( ( attr1 = value 1 attr2 = value2 )
( attr1 = value 1 attr2 = value2 )
).
- 利用已有的表赋值
table = corresponding #( table2 ).
读取内部表的值:
- 利用index
structure = table[ 1 ].
- 利用key
structure = table[ attr1 = value1
attr2 = value2 ].
可以直接读取某条记录某个属性值
attr1 = talbe[ 1 ]-attr1.
读取值时,有时不存在,所以一般都会包在try catch中读值
catch cx_sy_itab_line_not_found.
为提高performance,可以不用try catch, 直接用if判断
if line_exists( table[ 1 ] )
循环读值:
loop at table into structure from 1 to 5
endloop.
求表的总行数: num = lines ( table ).
修改内部表
loop at tablename into structure.
modify tablename ble from structure.
endloop.
删除表数据,保留内存,保留表结构
delete table tablename from structure.
删除表数据,删除内存,保留表结构
clear tablename.
delete类似于oracle中的delete
clear 类似于oracle中的truncate
ABAP中没有drop的概念,只要程序中不再用这个表变量,下次程序运行时,就不会有这个表的存在(垃圾回收)。
ABAP操作的都是internal表,不会去操作底层的database.
OPEN SQL
- 从表中读取单个数据
select single from tablename
fields attr1, attr2
where attr1 =
into @stucture.
- 从表中读取多条数据到另一张表
select from tablename
fields attr1, attr2
where attr1=..
into table @tablename
或者用
select from…
into @structure
tablename = value #(base tablename
structure)
endselect
定义方法之后,选中方法,CTRL+1 在implenation class中自动生成此代码框架。
class classname definition.
public section.
TYPES: BEGIN OF ts_attribute,
attribute TYPE string,
value TYPE string,
END OF ts_attribute,
tt_attributes TYPE STANDARD TABLE OF ts_attribute
WITH NON-UNIQUE KEY attribute.
methods:
classmethods:
private section.
data:
class-data:
constants:
endclass.
class classname implementation.
method methodname.
endmethod.
endclass.
定义class对象
data: class_obj type ref to class_name
OPEN SQL -----CDS View
DDL的name一般与cds view的name相同,与sql view的name不同
建立好文件DDL并激活后, 会在view下自动生成一个sql文件
除法/运算,只适用于float类型
cast(c.discount as abap.fltp)调用APBP类型,需要加abap.前缀
annotations:
label @EndUserText.label
tooltip: @ EndUserText.quickInfo
link: b.forcuram as amount @<Semantics.amount.currencyCode: 'CURRENCY',
b.forcurkey as currency @<Semantics.currencyCode: true,
currency_conversion(
amount => b.forcuram,
source_currency => b.forcurkey,
round => 'X',
target_currency => cast('USD' as abap.cuky),
exchange_rate_type => 'M',
exchange_rate_date => b.order_date,
error_handling => 'SET_TO_NULL') as amount @<Semantics.amount.currencyCode: 'CURRENCY',
cast('USD' as abap.cuky) as currency @<Semantics.currencyCode: true,
将cds view转换为odata service
@OData.publish: true
assocation不仅仅是jion,它是数据字典存储的一种可能的jion连接的metadata,实际的jion在association被使用时才创建。
定义table function如下,table function必须将clinet作为参数传入,returns中的字段即为返回的字段,取数逻辑放在AMDP Class的方法中。
文章来自于网络,如果侵犯了您的权益,请联系站长删除!