马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622 。
您需要 登录 才可以下载或查看,没有帐号?注册
x
【abap4 开发笔记(二)】
数据类型与数据对象 (ABAP BC400 P4-26记录) 类型: 1
用户或本地类型 2
数据字典ABAP 类型 包括: d
Date (YYYYMMDD) t
Time (HHMMSS) i
Integer f
float point number string
character String xstring
byte sequencs 十六进制 定义方法: TYPES: flag_type (1) TYPE C. 定义了一个长度为1的字符型类型 数据对象 一、
Elementary Date Object基本数据对象-------变量 TYPES:
flag_type (1)
TYPE
C. DATA: counter
TYPE
I,
* ABAP类型 flag
TYPE
flag_type,
* 本地用户类型,程序内部定义的 carrid
TYPE s_carr_id .
* 数据元素全局类型 数据字典中定义的 DATA varname1
TYPE
I . DATA varname2
LIKE
varname1 .
*继承 二、
Structures 结构 1、结构定义方式 2-1-1参考ABAP字典定义方式: 
TABLES
sbc400_carrier .
参照数据字典中预定义的结构 
DATA:
wa_carr
TYPE
scarr.
参照数据字典中透明表 
TABLES
sflight .
*
定义一个同数据库表同名的工作区,初始取第一条记录填充这个结构 备注:Transparent table There is a physical table on the database for each transparent table. The names of the physical tables and the logical table definition in the ABAP/4 Dictionary correspond. All business data and application data are stored in transparent tables. 可看看 例子: Dictionary Struture TYPE
SBC400FOCC 2-1-2、本地LOCAL PROGRAM结构类型定义 使用TYPE statement BEGIN OF
<structure type> … END OF < structure type > 如: TYPES: BEGIN OF flightinfo_type,
carrid
TYPE s_carr_id,
conn
TYPE s_conn_id,
fldate
TYPE s_date,
seatsmax
TYPE sflght-seatsmax,
seatssocc
TYPE sflght-seatsocc,
percentage(3) TYPE p DECIMALS 2,
END OF flightinfo_type.
* 以上方式定义的结构不能直接访问其字段或给字段赋值
再定义一个使用中的结构 DATA
<structure name> TYPE < structure name > . 如:DATA
wa_flightinfo type flightinfo_type.
* 注意冒号使用上的差别 2、访问结构中的字段 例子: … DATA:wa_scarr TYPE
scarr .
*参照数据字典中透明表定义结构 wa_scarr-carrid
= ‘LH’
. SELECT SINGLE * FROM scarr
INTO wa_scarr
WHERE CARRID = wa_scarr-carrid .
WRITE: / wa_scarr-carrid,
wa_scarr-carrname .
… 3、Copying Identically-Name Field Between Structures 使用 Move-corresponding 例子: DATA:wa_sflight TYPE sflight,
*根据现有的数据表定义结构
wa_sbc400focc TYPE
sbc400focc. *根据ABAP字典结构定义结构
SELECT SINGLE * FROM SFLIGHT
INTO WA_SFLIGHT .
IF sy-subrc = 0 . MOVE-CORRESPONDING wa_sflight TO wa_sbc400focc. WRITE: / wa_sbc400focc-CARRID,
wa_sbc400focc-CONNID,
wa_sbc400focc-FLDATE.
ENDIF . 三、
TERNAL TABLES 内表 为了在内存中处理多条记录,不能使用结构,而要使用INTERNAL TABLE 他有三种形式: 
STANDARD TABLES 
SORTED TABLES 
HASHED TABLE 我们将只讨论STANDARD TABLE 1、DECLARING INERTAL TABLES 参照字典内表类型 DATA itab_flightinfo TYPE abc400_t_sbc400focc
[with header line]. 2、程序中的本地表类型 SYNTAX (语法) TYPES:
flightinfo_type TYPE STANDARD TABLE OF sbc400focc WITH
NON-UNIQUE
KEY
carrid
connid
fldate . DATA
itab_flightinfo
TYPE
flightinfo_type
. 3、使用BEGIN…END OF <ITAB_NAME>******* DATA: BEGIN OF itab
OCCURS
0 ,
*Carrid like scarr-carrid,
*Carrname like scarr-carrname,
NAME(20), AGE TYPE I,
……
END OF itab . itab-NAME = 'Michael'. itab-AGE
= 25. APPEND itab.
*插入内表数据 itab-NAME = 'Gabriela'. itab-AGE
= 22. APPEND itab. 4、EXAMPLE : 将内表数据显示方法: 3-4-1、使用LOOP *先申明内表和工作区(结构)declaration of internal table and workarea DATA: itab_flightinfo TYPE sbc400_t_sbc400focc,
*参照字典内表
Wa_flightinfo Like line of itab_flightinfo .
*参照内表结构声明结构(工作区)
Itab_flightinfo
<----
Wa_flightinfo
LOOP AT itab_flightinfo into wa_flightinfo.
Write: /wa_flightinfo-carrid,
Write: /wa_flightinfo-connid, Write: /wa_flightinfo-seatsmax, Write: /wa_flightinfo-seatsocc, Write: /wa_flightinfo-percentage, ‘%’. Endloop . 3-4-2、使用INDEX LOOP AT itab_flightinfo INTO wa_flightinfo FROM
1 TO 5.
Write: /wa_flightinfo-carrid,
Write: /wa_flightinfo-connid, write: /wa_flightinfo-seatsmax, …… ENDLOOP.
*读单条符合条件的记录
READ TABLE
itab_flightinfo INTO wa_flightinfo
INDEX
3.
Write: /wa_flightinfo-carrid,
Write: /wa_flightinfo-connid, write: /wa_flightinfo-seatsmax, …… ‘%’. 3-4-3、使用KEYS LOOP AT itab_flightinfo INTO wa_flightinfo
WHERE carrid = ‘LH’ .
Write: /wa_flightinfo-carrid,
Write: /wa_flightinfo-connid, write: /wa_flightinfo-seatsmax, …… ENDLOOP.
*读单条符合条件的记录
READ TABLE
itab_flightinfo INTO wa_flightinfo With TABLE KEY carrid = ‘LH’, connid = ‘0400’ fldate = sy-datum . IF sy-subrc = 0 .
Write: /wa_flightinfo-carrid,
Write: /wa_flightinfo-connid, write: /wa_flightinfo-seatsmax, …… ‘%’. ENDIF . 5、内表数据排序 SORT itab_flightinfo BY percentage . 6、释放内存空间 *最后清空内表数据 CLEAR
itab . REFRESH
itab . *works like clear FREE
itab.
*deletes the internal table and releases the memory . 7、带表头的INTERNAL TABLE DATA
<itab>
TYPE
<itabletype>
[WITH
HEADER LINE] . *用工作区与带表头内表的操作区别: EXAMPLE: STRUTURES AND ASSIGNING VALUE /h
打开调试模式 program code : *&---------------------------------------------------------------------* *& Report
SAPBC400TSS_ITAB_LOOP
* *&
* *&---------------------------------------------------------------------* *&
* *&
* *&---------------------------------------------------------------------* REPORT
sapbc400tss_itab_loop
. DATA: it_spfli TYPE sbc400_t_spfli. * sbc400_t_spfli为数据字典内表 DATA: wa_spfli TYPE spfli.
*参照透明表定义结构(工作区) (tables wa_spfli .) START-OF-SELECTION.
SELECT * FROM spfli INTO TABLE it_spfli.
* at least one dataset selected
IF sy-subrc = 0.
* move each single dataset from internal table to structure WA_SPFLI in * order to write data on list
LOOP AT it_spfli INTO wa_spfli.
WRITE: / wa_spfli-carrid,
wa_spfli-connid,
wa_spfli-cityfrom,
wa_spfli-cityto,
wa_spfli-deptime,
wa_spfli-arrtime.
ENDLOOP.
ENDIF.
EXAMPLE 2: REPORT
sapbc400dds_select_sflight_tab
. DATA: wa_flight TYPE sbc400focc,
it_flight TYPE sbc400_t_sbc400focc.
PARAMETERS: pa_car TYPE s_carr_id. START-OF-SELECTION. *Select all datasets from database table SFLIGHT corresponding to *carrier PA_CAR
SELECT carrid connid fldate seatsmax seatsocc FROM sflight
INTO CORRESPONDING FIELDS OF wa_flight
WHERE carrid = pa_car.
*Calculate occupation of each flight
wa_flight-percentage =
100 * wa_flight-seatsocc / wa_flight-seatsmax.
* Insert single line into internal table
INSERT wa_flight INTO TABLE it_flight.
* Alternatively, you could use the APPEND statement, * if you are using standard tables: * *
APPEND wa_flight TO it_flight. *
ENDSELECT.
*sort internal table
SORT it_flight BY percentage.
*Create List from sorted internal table
LOOP AT it_flight INTO wa_flight.
WRITE: / wa_flight-carrid,
wa_flight-connid,
wa_flight-fldate,
wa_flight-seatsocc,
wa_flight-seatsmax,
wa_flight-percentage,'%'.
ENDLOOP.
补充: NODES:spfli,sflight. 定义一个与后面表名结构相同的工作区 原来用TABLES,现在此法也兼容。 更新数据库表格 UPDATE
1、使用工作区更新
UPDATE <database> FROM <wa>.
2、针对于某个字段的更新
UPDATE <database> SET <f1>=<values>… WHERE <condition>.
MODIFY <database> FROM <wa>. 根据Primary Key寻找数据库中符合条件的记录,若找到则更新相应字段 的值,若找不到则新增该记录。 DELETE
1、使用工作区删除
DELETE <database> FROM <wa>.
2、有条件地删除
DELETE FROM <database> WHERE <condition>. |