Sie sind auf Seite 1von 4

http://www.sapdev.co.

uk/tips/abap-inline-
declarations.htm

DATA and @DATA Inline ABAP declarations available from


release 7.40 to help make your code cleaner and more
readable

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 somthing 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 folowing 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.

ABAP News for Release 7.40 – Inline Declarations

Inline declarations are a new way of declaring variables and field symbols at operand
positions.

Data Declarations
In ABAP you have many operand positions, where the value of the operand is changed by
the statement. The most typical of these “write positions” is the left hand side lhs of an
assignment.
lhs = rhs.
But of course there are more. The data objects you can use at these write positions are
either writable formal parameters of the procedure you are working in or variables declared
with DATA in front of the statement.
In many cases the variables filled by a statement are helper variables that you only need
close to the statement. For each of  these helper variables you had to write a data
declaration with the DATAstatement and of course it was your task to give the variable an
adequate type.
Well, the operand type of most write positions is statically fixed and well known to the
compiler. And this is why ABAP can offer inline data declarations with Release 7.40. The
ingredients are so called declaration positions (write positions with fully known operand
type)  and the new declaration operator DATA(…).
Let’s look at some examples.

Declaration of a lhs-variable for a simple assignment


Before 7.40
DATA text TYPE string.
text = `…`.
With 7.40
DATA(text) = `…`.
Declaration of table work areas
Before 7.40
DATA wa like LINE OF itab.
LOOP AT itab INTO wa.   
  ...
ENDLOOP.
With 7.40
LOOP AT itab INTO DATA(wa).   
  ...
ENDLOOP.
Declaration of a helper variable
Before 7.40
DATA cnt TYPE i.
FIND … IN … MATCH COUNT cnt.
With 7.40
FIND … IN … MATCH COUNT DATA(cnt).
Declaration of a result
Before 7.40
DATA xml TYPE xstring.
CALL TRANSFORMATION … RESULT XML xml.
With 7.40
CALL TRANSFORMATION … RESULT XML DATA(xml).
Declaration of actual parameters
Before 7.40
DATA a1 TYPE …
DATA a2 TYPE …
oref->meth( IMPORTING p1 = a1
            IMPORTING p2 = a2
            … )
With 7.40
oref->meth( IMPORTING p1 = DATA(a1)
            IMPORTING p2 = DATA(a2)
            … ).
Declaration of reference variables for factory methods
Before 7.40
DATA ixml           TYPE REF TO if_ixml.
DATA stream_factory TYPE REF TO if_ixml_stream_factory.
DATA document       TYPE REF TO if_ixml_document.
ixml           = cl_ixml=>create( ).
stream_factory = ixml->create_stream_factory( ).
document       = ixml->create_document( ).
With 7.40
DATA(ixml)           = cl_ixml=>create( ).
DATA(stream_factory) = ixml->create_stream_factory( ).
DATA(document)       = ixml->create_document( ).
This example is my favorite. When working with class libraries as the iXML-Library you
don’t have to care about the data type of the reference variables too much any more. You
simply create them inline and use them. As you will see in the 7.40 version of the ABAP
Example Library, this feature has facilitated my writings of example programs considerably.

Field Symbols
For field symbols there is the new declaration operator FIELD-SYMBOL(…) that you can
use at exactly three declaration positions.
ASSIGN … TO FIELD-SYMBOL(<fs>).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).

ENDLOOP.
READ TABLE itab ASSIGNING FIELD-SYMBOL(<line>) …
I guess it is clear to you what happens here.

Outlook
In my upcoming blogs I will make use of inline declarations when introducing other new
features. Be prepared for code like this:
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(itab) = VALUE t_itab( ( 1 ) ( 2 ) ( 3 ) ).
Yes, this is ABAP 7.40 …

EJEMPLO CON ALV (definición de tipos dinámica)


go_grid1 es el grid que sea

DATA(ls_col) = VALUE lvc_s_col( fieldname = gc_c_motivo ).

DATA(ls_roid) = VALUE lvc_s_roid( row_id = 1 ).

go_grid1->set_current_cell_via_id( is_column_id = ls_col is_row_no = ls_roid ).

DATA(ls_col) = VALUE lvc_s_col( fieldname = ls_mod_cell-fieldname ). ZMAT_TRACT

DATA(ls_roid) = VALUE lvc_s_roid( row_id = ls_mod_cell-row_id ). 2

grid->set_current_cell_via_id( is_column_id = ls_col is_row_no = ls_roid ).