Dynamic Internal Table
Hi,
I have created a dynamic internal table using subroutine pool. But How do I use the dynamic table ?
(Please look at the Remark * of belows, the internal table ITAB is unknown to the main program.) Thanks
Regards,
Raymond
************************************************
* Sample program to generate internal table dynamically
************************************************
REPORT ZRAY28 .
PARAMETER ZMONAT TYPE BKPF-MONAT.
DATA: CODE(72) OCCURS 10,
WA_CODE(72) TYPE C, PROG(8) ,
MSG(120), LIN(3), WRD(10), OFF(3).
data: zindex(2) type n.
DATA: ZFIELD(10) TYPE C value 'ZFIELDNAME',
ZFIELDNAME(30) TYPE C.
APPEND 'PROGRAM SUBPOOL.' TO CODE.
APPEND 'FORM DYN1.' TO CODE.
APPEND 'TYPES: BEGIN OF ITAB_T,' TO CODE.
field-symbols: <xx>.
DO ZMONAT TIMES.
zindex = zindex + 1.
CONCATENATE 'FIELD' zindex INTO ZFIELDNAME.
CONCATENATE ZFIELDNAME 'type wertv8,' INTO ZFIELDNAME
separated by ' '.
assign (zfield) to <xx>.
APPEND <xx> TO CODE.
ENDDO.
APPEND 'END OF ITAB_T.' TO CODE.
APPEND 'DATA: ITAB TYPE STANDARD TABLE OF ITAB_T,' TO CODE.
APPEND 'WA_ITAB TYPE ITAB_T.' TO CODE.
APPEND 'ENDFORM.' TO CODE.
GENERATE SUBROUTINE POOL CODE NAME PROG
MESSAGE MSG LINE LIN WORD WRD OFFSET OFF.
IF SY-SUBRC <> 0.
WRITE:/ 'Error during generation in line', LIN, MSG,
'Word:', WRD, 'at offset', OFF.
ELSE.
WRITE:/ 'The name of the subroutine pool is', PROG.
skip 2.
PERFORM DYN1 IN PROGRAM (PROG).
*** I am stopped at here, please help ***
* WA_ITAB-FIELD01 = '100'.
* WA_ITAB-FIELD02 = '200'.
* ......
* .......
* WA_ITAB-FIELDXX = '900'.
* APPEND WA_ITAB TO ITAB.
*
* LOOP AT ITAB INTO WA_ITAB.
* WRITE:/ WA_ITAB-FIELD01, WA_ITAB-FIELD02.
* ENDLOOP.
ENDIF.
Your itab is being created in a seperate program and is not accessable from your main program.
That's the way subpools work.
Have a look here /forums/viewtopic.php?t=17261
for some code examples.
When you code a dynamic subroutine it basically must be self contained - ie the dynamic part of it cannot be accessed from the outside however you can pass parameters to that subroutine where you know the structure of thsoe parameters and then use those parameters to build your dynamic table.
R
Hi R,
Thank you for your reply. I have read your sample and found that all processes are done within the subroutine pool, which is different from my program. I want to process the dynamic table outside the subroutine pool.
I have read menu about Assign and Field-symbols command, but still no idea. Can you give me more hints ? Thank you very much.
Regards,
Raymond
I do remember some years ago a conversation about assigning field symbols to variables in other programs but I can't remember the details. I would have a rethink as to how you are going to achieve your aim.
I would look along the lines of keeping data in a single string perhaps delimited with some character. You can then use the 'SPLIT AT .... INTO TABLE' command with the rows in the itab being your fields.
Offsets and so on can also be computed assuming you have a fixed field length.... and 'records' start from 1....
Compute w_offset = ( w_record - 1 ) * w_length.
Move w_new_record to w_string+w_offset(w_length).
Move w_string+w_offset(w_length) to w_old_record.
What exactly are you trying to achieve ??
R
Hi R,
In the subroutine pool of my program, I created a internal table:
*** In subroutine subpool ***
types: begin of itab_t,
field01 type wertv8,
field02 type wertv8,
field03 type wertv8,
end of itab_t.
data: itab type standard table of itab_t,
wa_itab type itab_t.
The no. of fields (field01....fieldxx) is determined by the input value on screen.
After this table is created, I want to use this table itab like as defined in main program ZRAY28.
REPORT ZRAY28.
types: begin of itab_t,
field01 type wertv8,
field02 type wertv8,
field03 type wertv8,
end of itab_t.
data: itab type standard table of itab_t,
wa_itab type itab_t.
wa_itab-field01 = 100.
wa_itab-field02 = 200.
append wa_itab to itab.
...
Thanks
Regards,
Raymond
Ok..........
I may have the wrong end of the stick here. You have defined the table in your program ZRAY26 already ? If so why do you need to create a dynamic version ? You define it in SE11 as a structure and declare your itabs using that....
Or that you wish to use the resulting dynamic structure in your program ZRAY26.....
Well, bluntly you can't. The whole idea is that you are generating a subroutine. Data declarations in subroutine pools are not transferrable.
However, if you divide your program in 2 so that the first half writes out the generated program to a progam (see Insert Report). The 2nd program is the program that does all the work it must be fully self contained. You can declare the dynamic table as a type. You then submit that program from the first program.
Hi R,
Thank you for your replies. I will continue to try it.
Regards,
Raymond
Use the create dynamic internal table method. That will allow you to access the ITAB within your program
Hi jjwolos,
Thanks for your reply. What is dynamic table createion method ? I have searched the help using these key words but found nothing. Is it a FM?
Thanks.
Regards,
Raymond
you find sample code in Report "BCALV_TABLE_CREATE".
regards Hans
_________________
hans