注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

黑山老妖的博客

 
 
 

日志

 
 
关于我

主要用来存放收集的资料,几乎都是转贴的东西!感谢原作者!

ABAP4 开发笔记  

2007-02-15 16:15:45|  分类: ABAP |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

如何设置字段串和使用字段串?

DATA: BEGIN OF ADDRESS,

NAME(20) TYPE C,

STREET(20) TYPE C,

NUMBER TYPE P,

POSTCODE(5) TYPE N,

CITY(20) TYPE C,

END OF ADDRESS.该示例定义了一个长度为73的字段串ADDRESS。可以通过ADDRESS-NAME、ADDRESS-STREET等定址组件。可以将长字段串的声明收集在包括程序中。但是,如果频繁使用该数据结构,则最好将它们保存在ABAP/4词典中。

13.LIKE参数,语法如下:DATA LIKE .利用LIKE参数,可以将已定义数据对象的数据类型分配给变量。

LIKE参数常用于附属字段以临时保存数据库字段的内容。如果更改数据库字段的属性,则系统自动改写备份字段的属性。可将任何数据对象用于。利用LIKE参数,可以引用在ABAP/4词典中声明的数据对象的数据类型。如: DATA NUMBER_1 TYPE P.

DATA NUMBER_2 LIKE NUMBER_1.

DATA MYNAME LIKE SY-UNAME.

14.STATICS 语句:如果要在过程的运行时间之外仍保留某变量的数值,可以在该过程中利用STATICS语句定义该变量。STATICS语句是DATA语句的变异。语法如下: STATICS [] [] [].要在过程中将字段串定义为静态有效,请写:

STATICS: BEGIN OF ,

,

..............

END OF .

15.TABLES语句:TABLES 利用TABLES语句,可以创建称为表工作区的数据对象。表工作区是参考ABAP/4词典对象的字段串。是ABAP/4词典对象的名称,同时也是创建的表工作区的名称。表工作区组件的顺序和名称与在ABAP/4词典中声明的对象相同。要在ABAP/4词典中创建对象,步骤:Tools->ABAP Workbench->Development->ABAP Dictionary。表工作区提供一个接口,通过该接口可以将数据从数据库表加载到程序,或利用开放式SQL语句修改数据库表的内容。要显示表的组件及其数据类型的列表,请在ABAP/4编辑器的命令行中(选择“编辑->其它功能->命令输入”)键入“SHOW”。要定址表工作区的组件,可以将表名作为前缀,然后用连字符添加组件:dbtab>-例子:TABLES: SPFLI.

Select * FROM SPFLI.

WRITE: SPFLI-MANDT, SPFLI-CARRID, SPFLI-CONNID, ......

ENDSelect.示例中,TABLES语句创建表工作区SPFLI。SPFLI与ABAP/4词典中声明的数据库表SPFLI结构相同。在Select循环中,用属于数据库表SPFLI的行填充表工作区SPFLI。

15.TYPES语句:使用TYPES语句创建用户定义的基本数据类型和结构化数据类型。

TYPES: SURNAME(20) TYPE C,

BEGIN OF ADDRESS,

NAME TYPE SURNAME,

....

END OF ADDRESS.通过TYPES定义的类型不能直接使用,需要通过DATA进行定义其对应的变量后,使用其变量。

DATA: ADDRESS_1 TYPE ADDRESS,

ADDRESS_2 TYPE ADDRESS.

16.内表示例:下列程序是有关如何将结构化数据对象声明为内表的示例。

PROGRAM SAPMZTST.

TYPES: BEGIN OF MYSTRING,

NUMBER TYPE I,

NAME(10) TYPE C,

END OF MYSTRING.

TYPES MYTAB TYPE MYSTRING OCCURS 5.

DATA STRING TYPE MYSTRING.

DATA ITAB TYPE MYTAB.

STRING-NUMBER = 1. STRING-NAME = 'John'.

APPEND STRING TO ITAB.

STRING-NUMBER = 2. STRING-NAME = 'Paul'.

APPEND STRING TO ITAB.

STRING-NUMBER = 3. STRING-NAME = 'Ringo'.

APPEND STRING TO ITAB.

STRING-NUMBER = 4. STRING-NAME = 'George'.

APPEND STRING TO ITAB.

LOOP AT ITAB INTO STRING.

WRITE: / STRING-NUMBER,STRING-NAME.

ENDLOOP.该程序在屏幕上产生下列输出:

1 John

2 Paul

3 Ringo

4 George示例中,首先将数据类型MYSTRING定义为字段串。然后,根据字段串MYSTRING,用TYPES语句的OCCURS参数将数据类型MYTAB定义为内表。利用数据类型MYSTRING和MYTAB声明数据对象STRING和ITAB。然后逐行填充内表ITAB的字段。通过使用字段串STRING,在屏幕上显示ITAB的内容。

17.TYPE或TYPES所定义的变量不能直接赋值,只能通过DATA针对TYPE所定义的用户类型进行定义变量,然后使用这个变量。

18.如何确定数据对象的长度?

Data: text(8),len type i. //定义了两个变量

Describe field text length len. //将变量text的长度存放于变量len中

Write len. //打印变量text的长度值

19.如何确定数据对象的类型?

DATA: TEXT(8) TYPE N, TYP. //定义了两个变量TEXT和TYP

DESCRIBE FIELD TEXT TYPE TYP. //将变量TEXT的类型存放于变量TYP中 

WRITE TYP. //打印变量TEXT的类型

19.如何确定数据对象的输出长度?

Data: text type f, len type I, out-len type i. //定义了三个变量

Describe field text length len output-length out-len. //将变量text的长度存放于变量中

Write: len,out-len. //打印变量text的长度值

20.如何确定数据对象的小数位数?

Data: text type p decimals 2,dec. //定义了两个变量

Describe field text decimals dec. //将变量text的长度存放于变量中

Write: dec . //打印变量text的长度值

21.如何确定转换例程?

Data: text type n, exam type i. //定义了两个变量

Describe field text edit mask i. //将变量text的长度存放于变量中

Write: i . //打印变量text的长度值

三、 TERNAL TABLES 内表为了在内存中处理多条记录,不能使用结构,而要使用INTERNAL TABLE 他有三种形式:lSTANDARD TABLESlSORTED TABLESlHASHED 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 *******

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 TYPE [WITH HEADER LINE] .

*用工作区与带表头内表的操作区别:

EXAMPLE:

STRUTURES AND ASSIGNING VALUE

/h 打开调试模式

program code :

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,现在此法也兼容。 更新数据库表格nUpdate

1、使用工作区更新

Update FROM .

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

Update SET =… Where .nMODIFY FROM .根据Primary Key寻找数据库中符合条件的记录,若找到则更新相应字段的值,若找不到则新增该记录。nDelete

1、使用工作区删除

Delete FROM .

2、有条件地删除

Delete FROM Where .

abap4 SelectION-SCREEN

BC400

USER Dialog : SelectION SCREENS 涉及内容:概念: PARAMETERS -- SINGLE FIELDS

Select-OPTION --- VALUE SETS 事件:INITIALIZATION

AT SelectTION-SCREEN系统参数:MESSAGE 格式命令:

***注:程序显示行号参考最下图

SAMPLE 1 :SAPBC400DDD_Select_SINGLE

*&---------------------------------------------------------------------*

*& Report SAPBC400DDD_Select_SINGLE *

*& reading form database = Select SINGLE *

*&---------------------------------------------------------------------*

REPORT sapbc400ddd_select_single .

* workarea for datas from the database

DATA wa_scarr TYPE scarr.

PARAMETERS pa_car TYPE s_carr_id.

START-OF-SelectION.

* reading from database

Select SINGLE * FROM scarr

INTO CORRESPONDING FIELDS OF wa_scarr

Where

* specify all key fields in Where clause

* client is specified by the system

carrid = pa_car.

IF sy-subrc = 0.

* Output

WRITE: / wa_scarr-carrid,

wa_scarr-carrname,

wa_scarr-currcode.

else .

write: / text-001 color col_negative.

ENDIF.

SAMPLE 2:SAPBC400UDS_SEL_SCREEN

*&---------------------------------------------------------------------*

*& Report SAPBC400UDS_SEL_SCREEN *

*&---------------------------------------------------------------------*

REPORT sapbc400uds_sel_screen.

CONSTANTS: actvt_display TYPE activ_auth VALUE '03'.

DATA: wa_flight TYPE sbc400focc,

wa_sbook TYPE sbook.

PARAMETERS: pa_car like wa_flight-carrid.

* Data field for complex restrictions applied to connection id

Select-OPTIONS: so_con FOR wa_flight-connid.

* First event processed after leaving the selection screen

AT SelectION-SCREEN.

AUTHORITY-CHECK OBJECT 'S_CARRID'

ID 'CARRID' FIELD pa_car

ID 'ACTVT' FIELD actvt_display.

IF sy-subrc <> 0.

* Show selection screen again and show message in status bar

MESSAGE ID 'BC400' TYPE 'E' NUMBER '045' WITH pa_car.

ENDIF.

START-OF-SelectION.

Select carrid connid fldate seatsmax seatsocc FROM sflight

INTO CORRESPONDING FIELDS OF wa_flight

Where carrid = pa_car

AND connid IN so_con.

wa_flight-percentage =

100 * wa_flight-seatsocc / wa_flight-seatsmax.

WRITE: / wa_flight-carrid,

wa_flight-connid,

wa_flight-fldate,

wa_flight-seatsocc,

wa_flight-seatsmax,

wa_flight-percentage,'%'.

HIDE: wa_flight-carrid, wa_flight-connid, wa_flight-fldate.

ENDSelect.

CLEAR wa_flight.

AT LINE-SelectION.

IF sy-lsind = 1.

WRITE: / wa_flight-carrid, wa_flight-connid, wa_flight-fldate.

ULINE.

SKIP.

Select bookid customid custtype class order_date

smoker cancelled loccuram loccurkey

FROM sbook INTO CORRESPONDING FIELDS OF wa_sbook

Where carrid = wa_flight-carrid

AND connid = wa_flight-connid

AND fldate = wa_flight-fldate.

WRITE: / wa_sbook-bookid,

wa_sbook-customid,

wa_sbook-custtype,

wa_sbook-class,

wa_sbook-order_date,

wa_sbook-smoker,

wa_sbook-cancelled,

wa_sbook-loccuram CURRENCY wa_sbook-loccurkey,

wa_sbook-loccurkey.

ENDSelect.

ENDIF.

CLEAR: wa_flight, wa_sbook. 注意:

1、 各类事件出发的时间

2、 选择屏幕也是screen program 屏幕的一种

一个报表样例

*&---------------------------------------------------------------------*

*& Report ZSD_LONG *

*& *

*&---------------------------------------------------------------------*

*& 基础参考 下载 *

*& *

*&---------------------------------------------------------------------*

REPORT ZSD_LONG MESSAGE-ID zmsgtest line-size 120 .

****************************************************************

***数据段 声明表

tables : MAKT, "material table

VBAK, "sale order header table

VBAP, "sale order item table

KNA1. "address table

***数据段 声明结构以便内表使用

data: begin of it_addr occurs 0, "12/01 add

name1 like KNA1-name1, "com name

ORT01 like KNA1-ORT01, "city,

STRAS like KNA1-STRAS, " city,street

end of it_addr. "12/01 add

data : begin of it_header occurs 0,

vbeln like VBAK-vbeln, "sale order no

kunnr like VBAK-kunnr, "sale to

erdat like VBAK-erdat, "created date

addr like it_addr, "12/01 rewrite str可以嵌套

* addr like KNA1-ADRNR, "customer addr old

bukrs like vbak-BUKRS_VF, "com code

end of it_header.

data : begin of it_item occurs 0,

vbeln like vbap-vbeln, "sale order no

matnr like vbap-matnr, "material no

kwmeng like vbap-kwmeng, "sale item quanity

* zmeng like vbap-zmeng, "sale quanity

ZIEME like vbap-ZIEME, "unit

netwr like vbap-netwr, "sale netprice

WAERK like vbap-WAERK, "currency

maktx like makt-maktx, "material description

end of it_item.

data : l_list type i. "output linenum

****************************************************************

***输入屏幕 范围选择输入

Selection-screen begin of block b1 WITH FRAME title text-001.

Select-options: s_vbeln for VBAK-vbeln,

s_erdat for VBAK-erdat,

s_kunnr for VBAK-kunnr,

s_ernam for VBAK-ernam, "created by

s_bukrs for vbak-BUKRS_VF. "com code

***checkbox and required field

PARAMETER: P_run type d. " OBLIGATORY. OBLIGATORY:必须输入字段

PARAMETER: P_chk TYPE C AS CHECKBOX . "DEFAULT 'X'默认值.

Selection-screen end of block b1.

****************************************************************

***use customizing messag .when use at clause return has no use

**AT SelectION-SCREEN ON s_vbeln.

* if

** s_vbeln-high s_vbeln-low .

* message i000 .

* return.

* endif.

****************************************************************

***处理段 以Form调用

start-of-selection.

SET PF-STATUS '0001'. "用户菜单 12/01 改v1

*AUTHORITY CHECK object id 'in object' field 'the field to be auth'

AUTHORITY-CHECK OBJECT 'FW_ORD_BUK'

ID 'BUKRS' FIELD vbak-BUKRS_VF.

*简单控制运行时间 11/30改 v1

if P_chk <> 'X' and P_run = 0.

message i002.

else.

if P_chk = 'X'. "注意区分大小写

perform data_retrieve.

perform output_data.

else.

if P_run < sy-DATLO.

message i001.

else.

if P_run = sy-DATLO. "用户时间

* 没实现控制程序按照计划时间运行

perform data_retrieve.

perform output_data.

endif.

endif.

endif.

endif.

***注意:用户定义菜单sy-ucomm包含了双击事件 所以两者不能同时使用

*** 可以通过修改系统select事件 功能F2 达到选择行,从而响应的目的

*** 不再使用at LINE-SelectION与hide 这样问题解决一小半,

*** 用户菜单、双击可以共存,但不能根据行号来响应。

****用户自定义菜单 12/01 改 v1

*at user-command.

* CASE SY-UCOMM.

* WHEN 'quit'.

* leave program .

* when 'back'.

* return.

* when 'SELLINE'. "双击选择行单一响应

* write: / 'line one'.

* ENDCASE.

****用户自定义菜单 12/01 改v2 注意每次修改要激活,不然不可用

*** 通过case嵌套 只不使用at LINE-SelectION这样问题解决一半

*** 可响应不同行,但不能根据不同订单的行而不同响应

at user-command. "用户菜单事件sy-ucomm

CASE SY-UCOMM.

WHEN 'quit'. "quit 是菜单编辑中的CODE text则是菜单显示

* leave. "离开当前程序 同leave program作用.

* exit用于do while loop select的退出.

* leave program .

*离开当前整个程序,但可能还在当前当前事务.类似的leave screen

*其他:LEAVE LIST-PROCESSING.

*LEAVE TO LIST-PROCESSING [可以加 AND RETURN TO SCREEN scr]

* LEAVE TO CURRENT TRANSACTION.

*离开当前程序及所在事务,返回当前事务的前一事务处理.

*可以后带 AND SKIP FIRST SCREEN

LEAVE TO TRANSACTION 'SE38'.

*离开当前处理,转到指定事务se38事务

*可以后带 AND SKIP FIRST SCREEN 类似的leave to SCREEN scr.

when 'back'.

return. "无论任何状况,直接返回前一程序处理

when others.

case l_list. "case可以嵌套

when '1'.

write: / 'line one'.

when '2'.

write: / 'line 2'.

when '3'.

write: / 'line 3'.

when others.

write:/'wrong'.

endcase.

ENDCASE.

***method1 使用 双击事件响应

*AT LINE-SelectION.

* case l_list.

* when '1'.

* write: / 'line one'.

* when '2'.

* write: / 'line 2'.

* when '3'.

* write: / 'line 3'.

* when others.

* write:/'wrong'.

* endcase.

*&---------------------------------------------------------------------*

*& Form data_retrieve

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

*FORM data_retrieve .

*

* select vbeln kunnr erdat ernam

* appending table it_header "use table

* from vbak

* where vbeln in s_vbeln
<>

  评论这张
 
阅读(198)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018