Friday, December 08, 2006

ABAP学习

2. ABAP对象
2.1 面向对象基础
2.2 类和对象
2.3 方法
2.4 继承
2.5 接口
2.6 事件
2.1 面向对象基础
面向对象概念
ABAP Objects

2.2 类和对象
程序中的类定义 例8
定义系统全局类 例9
程序中的类实例
DATA objname TYPE REF TO classname
CREATE OBJECT objname
objname->comp
classname=>comp (CLASS-DATA静态)
Me=this

2.3 方法
参数的定义和传递
参数接口与功能模块极为相似
类创建器定义全局类时Method选项卡的Parame…
调用方法
CALL METHOD与功能模块的CALL FUNCTION类似
函数方法 例10
构造和析构方法
实例构造方法和对象构造方法

2.4 继承
继承的基本概念
单一继承,upcasting
多态性的继承实现
抽象类和最终类
2.5 接口
接口的定义与实现
接口引用 例11

2.6 事件
定义事件
声明事件(参数为值传递)、触发事件
事件处理方法 例12
声明事件处理方法、注册事件处理方法
3. 数据库操作
3.1 数据库基础
3.2 数据表维护
3.3 数据提取
3.4 数据更新
3.5 数据一致性
3.6 其他数据操作形式
3.1 数据库基础
在SAP系统中,数据库表的创建和维护一般通过数据字典进行,对表中数据的操作通常通过ABAP程序中的Open SQL语句完成。

SAP系统支持两种在程序中操作数据库的语句形式:ABAP的Open SQL语句以及特定数据库的自身SQL—Native SQL语句。

Open SQL使ABAP程序实现了数据库平台无关性,但Open SQL只能实现数据操纵(Data Manipulation查询更新),不能实现数据定义(Data Definition创建管理表、视图、索引)和数据控制(Data Control权限、安全性、一致性)

ABAP数据库操作的效率原则:减少数据库和应用服务器的负载

ABAP通过数据字典完成 Data Definition,通过 SAP LUW、SAP locking 、R/3权限实现 Data Control。


3.2 数据表维护
表字段
se11;数据库表与结构类型、表类型的区别:后二者是单纯的数据类型,前者还包含其他相关设定,如外码、索引等。
se11->database table->SPFLI 内容按钮 进入选择屏幕界面



3.2 数据表维护
技术设定 通过 Technical settings 按钮维护
操作数据库表时是否需要进行缓冲(对不需经常更新的数据库表提高后续查询效率)、是否要创建系统日志206
索引—仅包括部分字段的数据库表拷贝,通过 Indexes 按钮维护
主索引:只包含表关键字和指向整个数据条目的指针,系统自动生成
附属索引:根据其他字段生成索引,按指定方式排序,自行创建,协助主所以提高搜索效率。





3.3 数据提取
SELECT语句
选择单行数据 SELECT SINGLE
选择多行数据 循环选择/选择至内表 例13
指定查询条件 动态指定查询条件 例14 字符比较运算
select … where func like ‘EDIT#_%’ escape ‘#’.选定所有以”EDIT_”开始的功能名称
多表结合查询 select语句嵌套、for all entries选项、结合查询例15,16
组合查询结果 集函数、分组、升降序 例17, 18
其他格式说明 动态指定数据库表,指定数据区域,设置缓冲机制,
限定选择行数,se30性能分析
使用光标
3.4 更新数据
INSERT语句
插入单行:INSERT INTO dbtab VALUES wa.
插入多行:INSERT dbtab FROM TABLE itab.
UPDATE语句
更改单行:UPDATE dbtab set f1=g1…fn=gn where .
UPDATE dbtab FROM wa.
更改多行:UPDATE dbtab set f1=g1…fi=gi [where].
UPDATE dbtab FROM TABLE itab.
MODIFY语句 = INSERT + UPDATE,存在则更新,不存在则插入
DELETE语句 与INSERT类似

3.5 数据一致性
SAP LUW 与 DB LUW

在SAP系统中,两个数据一致状态中的时间间隔被称为LUW (Logical Unit of Work),每一个LUW都需要一个Commit或者Rollback作为结束标志。数据库LW与SAP系统无关,ABAP程序需要确定数据库LUW的触发时机。SAP系统中,数据库LUW是不能确保数据一致性的,每个对话步骤都会使一个数据库LUW结束,而很肯能用户通过多个行为才结束对所有数据库表的更新操作,一次SAP需要在数据库LUW的基础上发展自己的一致性维护机制,即SAP LUW,它可将多个数据库LUW进行捆绑,不进行真正的数据库修改。SAP LUW的两种数据库LUW捆绑机制,分别通过功能模块和子程序进行。
3.5 数据一致性
SAP数据锁定 se80->create->dictionary object->lock object

因为SAP的LUW概念独立于数据库LUW和对话步骤,所以SAP还需要自己定义数据库锁定机制。SAP LUW要求数据库对象的锁定在SAP LUW结束后释放,SAP提供一个逻辑数据锁定机制,基于系统特定的锁定服务应用服务器中的中心锁定表。

用户权限检查

SAP系统中,用户权限和其在程序中的评估是通过授权对象实现的。su21(多个对象被组合成一个类,分配给不同的应用)->BC_C->S_CARRID
授权对象不是一个个分配给系统用户,而是通过多个对象组合成的授权档案进行分配的。授权检查的类型:事务运行权限、程序运行权限、程序内部权限检查。

3.6 其他数据操作形式
文件接口
ABAP 程序读取数据库外部的数据源(即三层体系结构中的展示服务器(即程序员本机)和应用服务器),由于文件名和路径名在不同系统中有不同格式,文件操作也有平台相关性。可使用FILE事务创建逻辑文件名及路径名,ABAP程序中可使用功能模块FILE_GET_NAME得到当前平台可识别的文件名。

数据簇操作
簇(Cluster)是ABAP专有的数据存储形式,往往用于实现不同程序、事务之间方便的数据传递,没有必要单独创建数据库表,程序A中的数据可以暂存于某个位置的簇中,程序B运行时可以读取该簇,无需使用Open SQL。可存放数据簇的区域:ABAP内存、数据库表、跨事务缓存区。





一些例子:
11. 接口的实现
REPORT z_interface_demo.

interface status.
methods write.
endinterface. "status

class counter definition.
public section.
interfaces status.
methods increment.
private section.
data count type i.
endclass. "counter definition

class counter implementation.
method status~write.
write: / 'count in counter is', count.
endmethod. "status~write
method increment.
add 1 to count.
endmethod. "increment
endclass. "counter implementation

class bicycle definition.
public section.
interfaces status.
methods drive.
private section.
data speed type i.
endclass. "bicycle definition

class bicycle implementation.
method status~write.
write: / 'speed of bicycle is', speed.
endmethod. "status~write
method drive.
add 10 to speed.
endmethod. "drive
endclass. "bicycle implementation

data: count type ref to counter,
bike type ref to bicycle,
status type ref to status, "接口的引用作为工作区
status_tab type table of ref to status.

start-of-selection.

create object: count, bike.
do 5 times.
call method: count->increment,
bike->drive.
enddo.

append: count to status_tab,
bike to status_tab.

loop at status_tab into status.
call method status->write.
endloop.


12. 事件的触发及处理
REPORT z_class_counter_event.

CLASS vehicle DEFINITION INHERITING FROM object.
PUBLIC SECTION.
EVENTS: too_fast.
METHODS: accelerate,
show_speed.
PROTECTED SECTION.
DATA speed TYPE i.
ENDCLASS. "vehicle DEFINITION

CLASS vehicle IMPLEMENTATION.
METHOD accelerate.
speed = speed + 1.
IF speed > 5.
RAISE EVENT too_fast.
speed = 5.
ENDIF.
ENDMETHOD. "accelerate
METHOD show_speed.
WRITE: / 'Speed:', speed.
ENDMETHOD. "show_speed
ENDCLASS. "vehicle IMPLEMENTATION

CLASS handler DEFINITION.
PUBLIC SECTION.
METHODS handle_excess FOR EVENT too_fast OF vehicle.
ENDCLASS. "handler DEFINITION

CLASS handler IMPLEMENTATION.
METHOD handle_excess.
WRITE: / 'Speed can not be too fast.'.
* speed = 10.
ENDMETHOD. "handle_excess
ENDCLASS. "handler IMPLEMENTATION

DATA: o_vehicle TYPE REF TO vehicle,
o_handle TYPE REF TO handler.

START-OF-SELECTION.
CREATE OBJECT: o_vehicle, o_handle.
SET HANDLER o_handle->handle_excess FOR ALL INSTANCES.
DO 11 TIMES.
CALL METHOD o_vehicle->accelerate.
CALL METHOD o_vehicle->show_speed.
ENDDO.



13. 将数据选择至内表(多行)

DATA: BEGIN OF tab_type,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF tab_type.

DATA: spfli_tab1 TYPE TABLE OF spfli,
spfli_tab2 LIKE TABLE OF tab_type.

SELECT *
FROM spfli
INTO TABLE spfli_tab1
WHERE cityfrom = 'NEW YORK' AND cityto = 'LONDON'.

SELECT carrid connid
FROM spfli
INTO CORRESPONDING FIELDS OF TABLE spfli_tab2
WHERE cityfrom = 'NEW YORK' AND cityto = 'FRANKFURT'.

LOOP AT spfli_tab2 INTO tab_type.
WRITE: / tab_type-carrid,
tab_type-connid.
ENDLOOP.


14. 动态指定查询条件

DATA: cond(72) TYPE c,
itab LIKE TABLE OF cond,
city1(10) VALUE 'FRANKFURT',
city2(10) VALUE 'NEW YORK',
itab_spfli LIKE TABLE OF spfli WITH HEADER LINE.

CONCATENATE 'CITYFROM = ''' city1 '''' INTO cond.
APPEND cond TO itab.
CONCATENATE 'OR CITYFROM = ''' city2 '''' INTO cond.
APPEND cond TO itab.

SELECT *
FROM spfli
INTO TABLE itab_spfli
WHERE (itab).

LOOP AT itab_spfli.
WRITE: /
itab_spfli-cityfrom,
itab_spfli-cityto.
ENDLOOP.



15. 多表结合查询-select语句嵌套

DATA: wa_carrid TYPE spfli-carrid,
wa_connid TYPE spfli-connid,
wa_carrname TYPE scarr-carrname.

SELECT carrid connid
FROM spfli
INTO (wa_carrid, wa_connid)
WHERE cityfrom = 'NEW YORK'.
SELECT carrname
FROM scarr
INTO wa_carrname
WHERE carrid = wa_carrid.
WRITE / wa_carrname.
ENDSELECT. "内循环结束
ENDSELECT. "外循环结束

每当在表SPFLI中查询到一个符合条件的carrid值,系统就重新对SCARR进行一次
查询



16. 多表结合查询-for all entries

DATA: BEGIN OF wa_spfli,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF wa_spfli,
BEGIN OF wa_scarr,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
END OF wa_scarr,
spfli_tab LIKE TABLE OF wa_spfli.

SELECT carrid connid
FROM spfli
INTO TABLE spfli_tab
WHERE cityfrom = 'NEWYORK'.

SELECT carrid carrname
FROM scarr
INTO wa_scarr FOR ALL ENTRIES IN spfli_tab "比对spfli_tab中的每个元组
WHERE carrid = spfli_tab-carrid.
WRITE / wa_scarr-carrname.
ENDSELECT.

先将SPFLI中符合条件数据选进一个内表,再…








17. 组合查询

REPORT Z_SELECT_JOIN.

data: wa_carrid type spfli-carrid,
wa_connid type spfli-connid,
wa_carrname type scarr-carrname.

select spfli~carrid scarr~carrname spfli~connid
from spfli inner join scarr on scarr~carrid = spfli~carrid
into (wa_carrid, wa_carrname, wa_connid)
where spfli~cityfrom = 'NEW YORK'.
write: / wa_carrid, wa_carrname, wa_connid.
endselect.


18. 分组总计查询

data: carrid type sflight-carrid,
minimum type p decimals 2,
maximum type p decimals 2.

write: / 'Carrier ID',
at 15 'Minimum Price',
at 30 'Maximum Price'.

select carrid MIN( price ) MAX( price )
into (carrid, minimum, maximum)
from sflight
group by carrid
having MIN( price ) > 500
order by carrid descending.
write: / carrid, minimum, maximum.
endselect.





一些问题:

ABAP编辑器的自动缩进问题
'''的问题
nest是嵌套的意思
package创建的问题






Package
Related objects in the ABAP Workbench are grouped together in a package. The assignment of an object to a package is entered in the object directory (TADIR). The package determines the transport layer that defines the transport attributes of an object.
The packages are entered in the table TDEVC. They can be maintained in the following transactions:

* Transaction SE80 -> Enter package -> Double-click the package

* Transaction SM30 - Table/view name V_TDEVC

The packages are themselves objects of the ABAP Workbench. They belong to their own packages.
In contrast to its predecessor, the development class, a package has the following additional characteristics:

* Packages can be nested.

* Packages can contain their 'visible development objects' (visible outside of the package) in package interfaces.

* Packages can have use access defined for other package interfaces.

Use
When an ABAP Workbench object is created, the system prompts you to assign it to a package. The package should describe the area that the object belongs to.
The representation of the object tree in the ABAP Workbench (transaction SE80) uses the package as a navigation aid. If there are more than 100 objects of a certain type (that is, ABAP programs), the object tree can no longer be clearly represented and it becomes increasingly difficult to use the ABAP Workbench. In this case, we recommend creating new packages with the same transport layer and distributing the objects to the new packages on the basis of the areas they belong to.
The following naming conventions for packages determine the packages' functions:

* Package begins with A-S or U-X:
These packages are for SAP standard objects. Customer objects cannot be created in them. Changes to objects of these packages are recorded by the Transport Organizer (Request management) and can be transported (see field transport layer.

* Package begins with Y or Z:
Customer objects can be created in these packages. Changes to objects in these packages are recorded by the Transport Organizer (Request management). The objects can be transported to other SAP Systems (see the field transport layer ).

* Package begins with T (private test package):
When you create a package of this type, you can specify whether you want changes to be recorded. If so, objects that are edited are recorded in local requests by the Transport Organizer. This package does not belong to a transport layer. Objects can only be transported to other SAP Systems if a transport request is created.

* Package begins with $ (local package):
Changes to objects are not recorded by the Transport Organizer. The package does not belong to a transport layer. The objects cannot be transported.

* Package begins with a namespace prefix:
If you have reserved a namespace, then you can create packages (and other objects) whose names begin with the namespace prefix.
(Example of a namespace prefix /COMPANY/, example of a corresponding package /COMPANY/DEVCLASS)
Changes to these packages are recorded by the Transport Organizer, and can be transported.










Transport Layer in ABAP Workbench
Definition
The Change and Transport System supports the distribution of development work on large projects across multiple SAP Systems.
The packages in each development system are grouped into one transport layer.
The transport layer determines whether objects are assigned to a local or transportable change request.
Use
Each of your SAP development systems is assigned a transport layer as its standard transport layer. If you use Extended Transport Control, you can assign different standard transport layers to certain clients.
You can define at the most one consolidation target for each SAP System and transport layer.
When you create a package, it is assigned the standard transport layer of the SAP System.
If you want to assign a different transport layer to a package, you require the administration authorization for the Change and Transport System.
The objects in a package automatically have the transport attributes defined for the corresponding transport layer.

* If a consolidation route originating in their SAP System is defined, then the objects are assigned to a transportable request, and transported into the consolidation target when it is released.

* If a consolidation route is not defined, the objects are assigned to a local request, and are not transported.

Customizing settings are not assigned to a package. They have the transport attributes of the standard transport layer of the system or client.
It is best to assign a package a standard transport layer for which a consolidation route originating in the development system is defined.
To display and maintain the transport layers and routes, use the Transport Management System (transaction STMS).
Only the system adminstrator can make changes.
Caution:
The tables TSYST, DEVL, TWSYS, TASYS are no longer productive as of Release 4.0A and cannot be maintained.

No comments: