壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1269|回复: 1

sap部分开发笔记5

[复制链接]
发表于 2010/1/8 14:14:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服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、访问结构中的字段

例子:

DATAwa_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 .

3Copying Identically-Name Field Between Structures

使用 Move-corresponding

例子:

DATAwa_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

他有三种形式:

&#61548;
STANDARD TABLES

&#61548;
SORTED TABLES

&#61548;
HASHED TABLE

我们将只讨论STANDARD TABLE

1DECLARING 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、使用BEGINEND 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.

4EXAMPLE 将内表数据显示方法:

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.

补充:

NODESspfli,sflight.

定义一个与后面表名结构相同的工作区

原来用TABLES,现在此法也兼容。

更新数据库表格

&#61550;UPDATE


1
、使用工作区更新


UPDATE <database> FROM <wa>.


2
、针对于某个字段的更新


UPDATE <database> SET <f1>=<values>… WHERE <condition>.

&#61550;MODIFY <database> FROM <wa>.

     根据Primary Key寻找数据库中符合条件的记录,若找到则更新相应字段

     的值,若找不到则新增该记录。

&#61550;DELETE


1
、使用工作区删除


DELETE <database> FROM <wa>.


2
、有条件地删除


DELETE FROM <database> WHERE <condition>.
发表于 2011/5/8 08:40:48 | 显示全部楼层
有点晕呼呼的了  呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/30 17:07 , Processed in 0.016501 second(s), 14 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表