Sunteți pe pagina 1din 10

Dynamic Internal table

Skip to end of metadata

Added by Richa Khosla, last edited by Sandra Rossi on Nov 03, 2009 (vie chan!e"
sho comment
#o to start of metadata
Dynamic internal Table A $ynamic %nternal &able is an internal table ith variable n'mber of ros and col'mns, hich can
be defined d'rin! r'n time only(
A dynamic internal table is not declared in the pro!ram as static(
Some of the benefits of $ynamic internal table are)
*le+iblilty
,+tremely 'sef'l hen the n'mbers of col'mns - fields are not knon at the desi!n time - compile time(
Avoids red'ndancy

Some of the important attrib'tes of an internal table hich can be set dynamically are
.ol'mn position
*ield name of internal table field
.ol'mn idth
Reference field
Reference table
$ata type of the fields
$omain name of the fields
.heck table etc(
(*or the remainin! attrib'tes please refer the A/A0 str'ct're 12.3S3*.A&" 0rere4'isites of dynamic internal table are
Knoled!e of field symbols
Knoled!e on data references
Field symbols are placeholders or symbolic names for other fields( &hey are similar to dereference pointers in .(
*ield symbols allo yo' to)
Assi!n an alias to a data ob5ect
Adopt or chan!e the type and si6e of a data ob5ect dynamically at r'ntime
Set the offset and len!th for a strin! variably at r'ntime
Set a pointer to a data ob5ect that yo' determine at r'ntime (dynamic ASS%#N"
Access components of a str'ct're
&he statement ASS%#N f to 7fs8 assi!ns the field f to field symbol 7fs8( &he field symbol 7fs8 then 9points9 to the contents
of field f at r'ntime( &his means that all chan!es to the contents of f are visible in 7fs8 and vice versa(
:o' declare the field symbol 7fs8 'sin! the statement *%,1$;S:</=1S) 7fs8(



Data references are pointers to data ob5ects(
:o' can 'se data references to create data ob5ects dynamically( :o' can also create references to e+istin! data ob5ects(
:o' can only dereference a data reference 'sin! a special assi!nment to a field symbol(

:o' can create a data reference variable by 'sin!)
$A&A 7dref8 &:0, R,* &= $A&A(

&o create a data ob5ect dynamically d'rin! a pro!ram, yo' need a data reference variable and the folloin! statement)
.R,A&, $A&A 7dref8 &:0, 7type8>1%K, 7ob58(

&o access the contents of the data ob5ect to hich a data reference is pointin!, yo' m'st dereference it(
ASS%#N 7dref8;8? &= 7*S8( $ynamic internal tables can be created 'sin! )
&he class .13A123&A/1,3.R,A&, and the method .R,A&,3$:NA<%.3&A/1,(
,+port parameter) it3fieldcatalo!
%mport parameter) ep3table
,+ceptions) !enerate3s'bpool3dir3f'll @ A
=thers @ 2 Structure for Dynamic Internal Table Creation
A/A0 Str'ct're 12.3S3*.A&
&his str'ct're is 'sed to maintain the attrib'tes of each field of the $ynamic
%nternal &able s'ch as the fieldname, col'mn position, etc(

A/A0 &able type 12.3&3*.A&
&his table type has a line type of 12.3S3*.A&( &he field attrib'tes of all the
*ields are maintained in this table Steps to create a $ynamic %nternal &able
A( Data Definitions
$eclare a str'ct're of type lvc3s3fcat(
$eclare an internal table of type lvc3t3fcat (&he line type of this internal table is lvc3s3fcat"(
$eclare to data reference variables, one for the dynamic internal table (say drA" and the other for the ork area (say dr2"
$eclare field symbols of type Bref to dataB, Bany tableB and of type BanyB (say fsA, fs2 and fs3 respectively"( C 0op'late the
internal table ith fieldnames re4'ired for the dynamic internal table
a"(Assi!n the field name, field type, field idth, check table etc( to the str'ct're(
b"(Append the str'ct're to the internal table C Assi!n *ield;Symbol to dynamic internal table
i(e( Assi!n drA to 7fsA8 C .all the method .R,A&,3$:NA<%.3&A/1,
a"(0ass the internal table containin! the field attrib'tes as the e+port parameter(
b"(0ass the field symbol of the dynamic internal table as the import parameter( C &his creates the dynamic internal table
7fsA8 no refers to dynamic internal table that e anted to create at start( C Assi!n the data contents of 7fsA8 to a
field;symbol 7fs28 (dereferencin!"(
So 7fs28 no points to the dynamic internal table( C Ne+t step is to create a ork area for o'r dynamic internal
table(
.reate data dr2 like line of 7fs28( C A field;symbol to access that ork area
Assi!n dr2;8? to 7*S328( $rabacks of a $ynamic %nternal &able
0ro!rams ith many dynamic internal tables are less readable and they are less
sec're, since error cannot be detected by synta+ check, b't only by the r'ntime
system(
0erformance hen a $ynamic %nternal &able is 'sed ill not be as !ood as, hen a Static internal table is 'sed(
Sample 0ro!ram e+ample) ho to create a dynamic internal table
TYPE-POOLS : abap.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE
<dyn_!a>
<dyn_"#eld>.
DATA: dy_table TYPE REF TO data
dy_l#ne TYPE REF TO data
$"% TYPE l&%_'_"%at
#"% TYPE l&%_t_"%at.
SELE(TION-S(REEN BE)IN OF BLO(* b+ ,IT- FRAME.
PARAMETERS: p_table./01 TYPE % DEFA2LT 3T00+3.
SELE(TION-S(REEN END OF BLO(* b+.

START-OF-SELE(TION.
PERFORM 4et_'t56%t65e.
PERFORM %5eate_dyna7#%_#tab.

8888888(5eate' a dyna7#% #nte5nal table888888888
PERFORM 4et_data.
PERFORM !5#te_96t.
8:---------------------------------------------------------------------8
8: F957 4et_'t56%t65e
8:---------------------------------------------------------------------8
FORM 4et_'t56%t65e.
DATA : #deta#l' TYPE abap_%97pde'%5_tab
$deta#l' TYPE abap_%97pde'%5.
DATA : 5e"_table_de' TYPE REF TO %l_abap_'t56%tde'%5.
8 )et t;e 't56%t65e 9" t;e table.
5e"_table_de' <=
%l_abap_typede'%5=>de'%5#be_by_na7e. p_table 1.
#deta#l'>? = 5e"_table_de'->%97p9nent'>?.
LOOP AT #deta#l' INTO $deta#l'.
(LEAR $"%.
$"%-"#eldna7e = $deta#l'-na7e .
8 (955e%t#9n by Pa6l R9be5t O%t @A @00B +C:0D
8 $"%-datatype = $deta#l'-type_E#nd.
(ASE $deta#l'-type_E#nd.
,-EN 3(3.
$"%-datatype = 3(-AR3.
,-EN 3N3.
$"%-datatype = 3N2M(3.
,-EN 3D3.
$"%-datatype = 3DATE3.
,-EN 3P3.
$"%-datatype = 3PA(*3.
,-EN OT-ERS.
$"%-datatype = $deta#l'-type_E#nd.
END(ASE.
$"%-#nttype = $deta#l'-type_E#nd.
$"%-#ntlen = $deta#l'-len4t;.
$"%-de%#7al' = $deta#l'-de%#7al'.
APPEND $"% TO #"%.
ENDLOOP.
ENDFORM. F4et_'t56%t65e
8:---------------------------------------------------------------------8
8: F957 %5eate_dyna7#%_#tab
8:---------------------------------------------------------------------8
FORM %5eate_dyna7#%_#tab.
8 (5eate dyna7#% #nte5nal table and a''#4n t9 FS
(ALL MET-OD %l_al&_table_%5eate=>%5eate_dyna7#%_table
EGPORTIN)
#t_"#eld%atal94 = #"%
#_len4t;_#n_byte = 3G3 Fadded by Pa6l R9be5t O%t @A @00B +C:0D
IMPORTIN)
ep_table = dy_table.
ASSI)N dy_table->8 TO <dyn_table>.
8 (5eate dyna7#% !95E a5ea and a''#4n t9 FS
(REATE DATA dy_l#ne LI*E LINE OF <dyn_table>.
ASSI)N dy_l#ne->8 TO <dyn_!a>.
ENDFORM. F%5eate_dyna7#%_#tab

8:---------------------------------------------------------------------8
8: F957 4et_data
8:---------------------------------------------------------------------8
FORM 4et_data.
8 Sele%t Data "597 table.
SELE(T 8 INTO TABLE <dyn_table>
FROM .p_table1.
ENDFORM. F4et_data

8:---------------------------------------------------------------------8
8: F957 !5#te_96t
8:---------------------------------------------------------------------8
FORM !5#te_96t.
LOOP AT <dyn_table> INTO <dyn_!a>.
DO.
ASSI)N (OMPONENT 'y-#nde$
OF STR2(T2RE <dyn_!a> TO <dyn_"#eld>.
IF 'y-'6b5% <> 0.
EGIT.
ENDIF.
IF 'y-#nde$ = +.
,RITE:H <dyn_"#eld>.
ELSE.
,RITE: <dyn_"#eld>.
ENDIF.
ENDDO.
ENDLOOP.
ENDFORM. F!5#te_96t
R,0=R& 6maschl3create3data3dynamic (

&:0,;0==1S) slis(

$A&A) it3fcat &:0, slis3t3fieldcat3alv,
is3fcat 1%K, 1%N, =* it3fcat(
$A&A) it3fieldcat &:0, lvc3t3fcat,
is3fieldcat 1%K, 1%N, =* it3fieldcat(
$A&A) ne3table &:0, R,* &= data(
$A&A) ne3line &:0, R,* &= data(
*%,1$;S:</=1S) 7l3table8 &:0, AN: &A/1,,
7l3line8 &:0, AN:,
7l3field8 &:0, AN:(

Build feldcat
.A11 *DN.&%=N BR,DS,3A123*%,1$.A&A1=#3<,R#,B
,E0=R&%N#
i3str'ct're3name @ BS:S&B
.FAN#%N#
ct3fieldcat @ it3fcatGH(
1==0 A& it3fcat %N&= is3fcat IF,R, N=& repte+t3ddic %S initial(
<=2,;.=RR,S0=N$%N# is3fcat &= is3fieldcat(
is3fieldcat;fieldname @ is3fcat;fieldname(
is3fieldcat;ref3field @ is3fcat;fieldname(
is3fieldcat;ref3table @ is3fcat;ref3tabname(
A00,N$ is3fieldcat &= it3fieldcat(
,N$1==0(

Create a new Table
.A11 <,&F=$ cl3alv3table3create@8create3dynamic3table
,E0=R&%N#
it3fieldcatalo! @ it3fieldcat
%<0=R&%N#
ep3table @ ne3table(

Create a new Line with the same structure of the table.
ASS%#N ne3table;8? &= 7l3table8(
.R,A&, $A&A ne3line 1%K, 1%N, =* 7l3table8(
ASS%#N ne3line;8? &= 7l3line8(

Test it...
$= 30 &%<,S(
ASS%#N .=<0=N,N& BSD/R.B =* S&RD.&DR, 7l3line8 &= 7l3field8(
7l3field8 @ sy;inde+(
%NS,R& 7l3line8 %N&= &A/1, 7l3table8(
,N$$=(

1==0 A& 7l3table8 ASS%#N%N# 7l3line8(
ASS%#N .=<0=N,N& BSD/R.B =* S&RD.&DR, 7l3line8 &= 7l3field8(
IR%&, 7l3field8(
,N$1==0(

%f yo' !ive the inp't as 3(((%& ill create 3 col'mns((

0ARA<,&,RS) p3inp't &:0, i =/1%#A&=R:(

S&AR&;=*;S,1,.&%=N(

$A&A) v3fieldname &:0, char30(
$A&A) v3char &:0, n'mcJ(
$A&A) it3fldcat &:0, lvc3t3fcat(
$A&A) a3it3fldcat 1%K, 1%N, =* it3fldcat(
$A&A) !p3table &:0, R,* &= data(

*%,1$;S:</=1S) 7!t3table8 &:0, table(

$= p3inp't &%<,S(

v3fieldname @ B.=1B(
v3char @ sy;inde+(

.=N.A&,NA&, v3fieldname v3char %N&= v3fieldname(
.=N$,NS, v3fieldname(

.1,AR a3it3fldcat(
a3it3fldcat;fieldname @ v3fieldname(
a3it3fldcat;datatype @ B.FARB(
a3it3fldcat;o'tp'tlen @ K(
a3it3fldcat;intlen @ K(
A00,N$ a3it3fldcat &= it3fldcat (

,N$$=(

Internal table creation..
.A11 <,&F=$ cl3alv3table3create@8create3dynamic3table
,E0=R&%N# it3fieldcatalo! @ it3fldcat
%<0=R&%N# ep3table @ !p3table(

ASS%#N !p3table;8? &= 7!t3table8(

.F,.K sy;s'brc @ 0(

$A&A) IA &:0, R,* &= $A&A(

Work area for the dynamic internal table.
.R,A&, $A&A IA 1%K, 1%N, =* 7!t3table8(

IR%&,) - B$ynamic internal table createdB(
Steps to Create Dynamic ITAB
&o create a dynamic internal table, e need to)
1. #ather all the .omponents
2. #enerate a &ype from this components
3. #enerate a &able &ype from this created type
4. .reate a $ata reference of this &able &ype
5. Assi!n this data reference to the *ield;Symbol of table type( &his *ield;symbol ill act as
o'r dyanmic internal table
Demo Application
%n todayLs e+ample e ill see ho to)
A( .reate $ynamic internal table
2( Irite dynamic select 4'ery to data into this dynamic table
3( .han!e the contents of this dynamic internal table
J( #enerate the A12 display 'sin! SA12 model for this dynamic internal table( ,+plore ho
to create A12 'sin! SA12 from &'torials 8 SA12 &able $isplay
Ie ill provide the selection of the period for hich 'ser ants to !enerate an o'tp't(
/ased on the entered periods e ill create a dynamic type contain! the KS&AR (.ostin!
,lement" and Amo'nt fields for the month( After creatin! the dynamic type, e ill create a
dynamic table type( Dsin! this table type e ill create a reference of the data( *rom this
data reference e ill assi!n the internal table to field;symbols(
So, letLs see the code snippet to create $ynamic %&A/)
*&--------------------------------------------------------------------
-*
*& This Code snippet shows how to
*& Create Dynamic Internal Table
*& Dynamic Selection of data
*& Accessing Dynamic data selection
*& Displaying Dynamic internal table in ALV
*&--------------------------------------------------------------------
-*
5ep95t Idyna7#%_#tab.
*
* !isiting Table type
TYPES: BE)IN OF ty_E'ta5
E'ta5 TYPE E'ta5
END OF ty_E'ta5.
*
* Dynamic Table creation
DATA: l9_'t56%t TYPE REF TO %l_abap_'t56%tde'%5
l9_ele7ent TYPE REF TO %l_abap_ele7de'%5
l9_ne!_type TYPE REF TO %l_abap_'t56%tde'%5
l9_ne!_tab TYPE REF TO %l_abap_tablede'%5
l9_data TYPE REF TO data
lt_%97p TYPE %l_abap_'t56%tde'%5=>%97p9nent_table
lt_t9t_%97p TYPE %l_abap_'t56%tde'%5=>%97p9nent_table
la_%97p LI*E LINE OF lt_%97p
l"_79nt;' TYPE 79nat
l"_56n_79n TYPE 79nat.
*
* Dynamic Selection fields
TYPES: BE)IN OF ty_"#eld'
"#eld TYPE %;a5/0
END OF ty_"#eld'.
*
DATA: lt_"#eld' TYPE STANDARD TABLE OF ty_"#eld'
la_"#eld' TYPE ty_"#eld'.
*
* field symbols to access the dynamic table
FIELD-SYMBOLS: <"_tab> TYPE ANY TABLE
<"_l#ne> TYPE ANY
<"_"#eld> TYPE ANY.
*
* Selection Screen
PARAMETERS: p_79n_"5 TYPE 79nat
p_79n_t9 TYPE 79nat.
*
START-OF-SELE(TION.
*
*"*"*###############Dynamic Internal Table########################*"*"
*
* $# %etting Compoents from e!isting type
l9_'t56%t <= %l_abap_typede'%5=>de'%5#be_by_na7e. 3TY_*STAR3 1.
lt_%97p = l9_'t56%t->4et_%97p9nent'. 1.
APPEND LINES OF lt_%97p TO lt_t9t_%97p.
*
* &# Adding re'(ired fields based on the single data element
* Determining )(mber of fields
l"_79nt;' = . p_79n_t9 - p_79n_"5 1 J +.
l"_56n_79n = p_79n_"5.
*
DO l"_79nt;' TIMES.
*
* lement Description
l9_ele7ent <= %l_abap_ele7de'%5=>de'%5#be_by_na7e. 3,T)GGG3 1.
*
* *ield name
(ON(ATENATE 3,T)03 l"_56n_79n INTO la_%97p-na7e.
*
* *ield type
la_%97p-type = %l_abap_ele7de'%5=>4et_p.
p_len4t; = l9_ele7ent->len4t;
p_de%#7al' = l9_ele7ent->de%#7al' 1.
*
* *illing the component table
APPEND la_%97p TO lt_t9t_%97p.
(LEAR: la_%97p.
*
l"_56n_79n = l"_56n_79n J +.
ENDDO.
*
* +# Create a )ew Type
l9_ne!_type = %l_abap_'t56%tde'%5=>%5eate. lt_t9t_%97p 1.
*
* ,# )ew Table type
l9_ne!_tab = %l_abap_tablede'%5=>%5eate.
p_l#ne_type = l9_ne!_type
p_table_E#nd = %l_abap_tablede'%5=>tableE#nd_'td
p_6n#K6e = abap_"al'e 1.
*
* -# data to handle the new table type
(REATE DATA l9_data TYPE -ANDLE l9_ne!_tab.
*
* .# )ew internal table in the fieldsymbols
ASSI)N l9_data->8 TO <"_tab>.
*
*"*"*###############Dynamic Selection#############################*"*"
*
* *illing (p the table for the Selection fields of Select /(ery
LOOP AT lt_t9t_%97p INTO la_%97p.
la_"#eld'-"#eld = la_%97p-na7e.
APPEND la_"#eld' TO lt_"#eld'.
(LEAR: la_%97p la_"#eld'.
ENDLOOP.
*
* Selecting data
SELE(T .lt_"#eld'1
INTO TABLE <"_tab>
FROM %9'p
2P TO +0 RO,S.
*
*"*"*###############Accessing dynamic table#######################*"*"
*
LOOP AT <"_tab> ASSI)NIN) <"_l#ne>.
ASSI)N (OMPONENT 3,T)00D3 OF STR2(T2RE <"_l#ne> TO <"_"#eld>.
<"_"#eld> = 3+00.003.
ENDLOOP.
*
*
*"*"*###############Displaying (sing SALV model###################*"*"
*
*
DATA: l9_al& TYPE REF TO %l_'al&_table.
*
TRY.
%l_'al&_table=>"a%t95y.
EGPORTIN)
l#'t_d#'play = abap_"al'e
IMPORTIN)
5_'al&_table = l9_al&
(-AN)IN)
t_table = <"_tab> 1.
(AT(- %$_'al&_7'4 .
ENDTRY.
*
l9_al&->d#'play. 1.

S-ar putea să vă placă și