Pages

Tuesday, June 7, 2016

Inline ABAP declarations


Inline Declaration with release 7.4 



Inline ABAP DATA declarations are a new concept introduced in release 7.4 which allows you to declare your internal table variables or work areas within the code that uses them. This is done simply by adding the DATA(wa_data) or @DATA(it_data) statements.
The easiest way to describe this is to show you a very simple before after ABAP code example.
Pre 7.40 you would declare a work area and read data into it something like this


DATA: it_ekko TYPE STANDARD TABLE OF EKKO,
wa_ekko TYPE EKKO.
READ IT_EKKO INTO WA_EKKO.


Using the new method you would simply just using the following code:


READ TABLE it_ekko INDEX 1 INTO DATA(wa_ekko_new).


Hopefully the above example gives you an idea of how this new functionality can streamline your ABAP code and make it more readable. Also gives you the basics so you can have a play yourself. Now onto the @DATA statement....




The @DATA statement- Service Pack 08

The @DATA statement takes this a step further, allowing you to automatically inline declare an internal table to hold values you are selecting from a database table, using the SELECT statement. The only problem with this functionality is that it doesn't come in until Service Pack 08 or SP09. I have tried SP07 and it is definitely not in that and is there by SP09.
If you had a coding example where you declare an internal table to select values into it you would have some code that looked something like this.



TYPES: BEGIN OF t_ekko,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
statu TYPE ekpo-statu,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
  CELLCOLOR TYPE LVC_T_SCOL,
 END OF t_ekko.
DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
      wa_ekko TYPE t_ekko.
select ebeln ebelp statu aedat matnr menge meins netpr peinh
   up to 10 rows
  from ekpo
  into CORRESPONDING FIELDS OF TABLE it_ekko.


Using the new @DATA statement you could essentially replace all this with the following ABAP code, which automatically create an internal table with the correct field structure and drops the selected values into it. AS well as the @DATA note the ,'s after each field in the SELECT.


select ebeln, ebelp, statu, aedat, matnr, menge, meins, netpr, peinh
    up to 10 rows
    from ekpo    into TABLE @DATA(it_ekko_new).

This is more like it and if you combine the two examples together you get quite a neat and efficient looking section of code. It essentially removes all the ABAP declaration lines of code without adding much at all to SELECT and READ statements.

select ebeln, ebelp, statu, aedat, matnr, menge, meins, netpr, peinh    up to 10 rows
    from ekpo    into TABLE @DATA(it_ekko_new).
READ TABLE it_ekko_new INDEX 1 INTO DATA(wa_ekko_new2).
 WRITE:/ wa_ekko_new2.

1 comment: