Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

determine data type dynamically

Former Member
0 Likes
624

Hi there. I've got a question. Is there some possibility to declare data type during program flow depending on some conditions

For example:


IF ( cond1 ) .
  DATA: var LIKE someitab1 .
ELSEIF ( cond2 ) .
  DATA: var LIKE someitab2 .
ENDIF .

I'll be thankful for help . Greetings .

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
600

for this u can use field symbols

<fs-field> type any

5 REPLIES 5
Read only

Former Member
0 Likes
601

for this u can use field symbols

<fs-field> type any

Read only

Former Member
0 Likes
600

HI,

We can declare the variables based on Conditions.

Read only

Former Member
0 Likes
600

Check out this code:

PARAMETERS: P_TABLE LIKE DD03L-TABNAME.

FIELD-SYMBOLS: <FS_TABLE> TYPE TABLE,

<WA_TABLE> TYPE ANY.

DATA: XTABLE TYPE REF TO DATA." It tells compiler that the data object type will be defined at runtime

CREATE DATA XTABLE TYPE TABLE OF (P_TABLE)." It creates data XTABLE at runtime

ASSIGN XTABLE->* TO <FS_TABLE>." before using this field symbol in LOOP, its must be assigned

SELECT * FROM (P_TABLE) INTO TABLE <FS_TABLE> ORDER BY PRIMARY KEY.

LOOP AT <FS_TABLE> ASSIGNING <WA_TABLE>.

ENDLOOP.

Read only

former_member203501
Active Contributor
0 Likes
600

if test = 'name1'.

field-symbols: <table1> type table ,

<field1> type any .

elseif test = 'name2'.

field-symbols: <table2> type table ,

<field2> type any .

endif .

Read only

Former Member
0 Likes
600

It will be better to define data and than assign it to field symbol at runtime.

Data : someitab1 type table of itab1, 
          someitab2 type table of itab2.

field-symbols : <f_itab> type table.

IF ( cond1 ).
assign SOMEITAB1[] to <f_itab>.
ELSEIF ( cond2 ) .
assign SOMEITAB2[] to <f_itab>.
ENDIF.

Similarly you can have condition for variable. Above example is for internal tables.

Regards,

Mohaiyuddin