一些常用ABAP program 的例子

导读:
  1. 接口的实现
  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.
  2. 事件的触发及处理
  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.
  3. 将数据选择至内表( 多行)
  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.
  4. 动态指定查询条件
  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.
  5. 多表结合查询-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进行一次
  查询
  6. 多表结合查询-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.
  7. 组合查询
  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.
  8. 分组总计查询
  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.

本文转自
http://qiujun.itpub.net/post/24006/282021
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值