09-03-2007 8:51 AM
Hi,
I want to loop at internal table whose name can be dynamic (i can pass as variable).
loop at <dyn_table>.
endloop.
How do I pass different values to <dyn_table>?
Please help.
Thanks.
09-03-2007 8:56 AM
Hi,
You can use field symbols for this.
Check the following example,
the program is to handle dynamic internal table you can use this for your problem.
TYPE-POOLS: slis.
DATA: it_fcat TYPE slis_t_fieldcat_alv,
is_fcat LIKE LINE OF it_fcat.
DATA: it_fieldcat TYPE lvc_t_fcat,
is_fieldcat LIKE LINE OF it_fieldcat.
DATA: new_table TYPE REF TO data,
new_line TYPE REF TO data.
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,
<l_line> TYPE ANY,
<l_field> TYPE ANY.
Build fieldcat
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SYST'
CHANGING
ct_fieldcat = it_fcat[].
LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.
MOVE-CORRESPONDING is_fcat TO is_fieldcat.
is_fieldcat-fieldname = is_fcat-fieldname.
is_fieldcat-ref_field = is_fcat-fieldname.
is_fieldcat-ref_table = is_fcat-ref_tabname.
APPEND is_fieldcat TO it_fieldcat.
ENDLOOP.
Create a new Table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat
IMPORTING
ep_table = new_table.
Create a new Line with the same structure of the table.
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.
Test it...
DO 30 TIMES.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
<l_field> = sy-index.
INSERT <l_line> INTO TABLE <l_table>.
ENDDO.
LOOP AT <l_table> ASSIGNING <l_line>.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
WRITE <l_field>.
ENDLOOP.
<b>Reward if helpful.</b>
09-03-2007 8:54 AM
Hi Apps
This is example to loop dynamic table.
FIELD-SYMBOLS : <GT_1> TYPE TABLE ,
<GS_1> TYPE ANY ,
<FS> TYPE ANY .
<F_1> TYPE ANY .
DATA: BEGIN OF GT_FLD OCCURS 0,
FIELDNAME TYPE FIELDNAME,
END OF GT_FLD.
DATA: BEGIN OF GT_1 OCCURS 0,
KUNNR LIKE BSID-KUNNR,
DMBTR LIKE BSID-DMBTR,
END OF GT_1.
DATA: GD_REF1 TYPE REF TO DATA,
GT_ALV_CAT TYPE TABLE OF LVC_S_FCAT WITH HEADER LINE.
START-OF-SELECTION.
REFRESH: GT_FLD.
REFRESH GT_ALV_CAT.
MOVE 'GJAHR' TO GT_FLD. APPEND GT_FLD.
MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.
MOVE 'GJAHR' TO GT_ALV_CAT-REF_FIELD.
MOVE 'GJAHR' TO GT_ALV_CAT-FIELDNAME.
APPEND GT_ALV_CAT.
MOVE 'KUNNR' TO GT_FLD. APPEND GT_FLD.
MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.
MOVE 'KUNNR' TO GT_ALV_CAT-REF_FIELD.
MOVE 'KUNNR' TO GT_ALV_CAT-FIELDNAME.
APPEND GT_ALV_CAT.
MOVE 'DMBTR' TO GT_FLD. APPEND GT_FLD.
MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.
MOVE 'DMBTR' TO GT_ALV_CAT-REF_FIELD.
MOVE 'DMBTR' TO GT_ALV_CAT-FIELDNAME.
APPEND GT_ALV_CAT.
make dynamic table to use alv method
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING IT_FIELDCATALOG = GT_ALV_CAT[]
IMPORTING EP_TABLE = GD_REF1.
ASSIGN GD_REF1->* TO <GT_1>.
SELECT (GT_FLD) INTO TABLE <GT_1>
FROM BSID
WHERE GJAHR = 2007 AND
BUKRS = 'C5A0'.
assign data to field
LOOP AT <GT_1> ASSIGNING <GS_1>.
CLEAR GT_1.
ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <GS_1> TO <F_1>.
MOVE <F_1> TO GT_1-KUNNR.
ASSIGN COMPONENT 'DMBTR' OF STRUCTURE <GS_1> TO <F_1>.
MOVE <F_1> TO GT_1-DMBTR.
APPEND GT_1.
ENDLOOP.
Regards
Wiboon
Message was edited by:
Wiboon Chaiyabutsakul
09-03-2007 8:56 AM
Hi,
You can use field symbols for this.
Check the following example,
the program is to handle dynamic internal table you can use this for your problem.
TYPE-POOLS: slis.
DATA: it_fcat TYPE slis_t_fieldcat_alv,
is_fcat LIKE LINE OF it_fcat.
DATA: it_fieldcat TYPE lvc_t_fcat,
is_fieldcat LIKE LINE OF it_fieldcat.
DATA: new_table TYPE REF TO data,
new_line TYPE REF TO data.
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,
<l_line> TYPE ANY,
<l_field> TYPE ANY.
Build fieldcat
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SYST'
CHANGING
ct_fieldcat = it_fcat[].
LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.
MOVE-CORRESPONDING is_fcat TO is_fieldcat.
is_fieldcat-fieldname = is_fcat-fieldname.
is_fieldcat-ref_field = is_fcat-fieldname.
is_fieldcat-ref_table = is_fcat-ref_tabname.
APPEND is_fieldcat TO it_fieldcat.
ENDLOOP.
Create a new Table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat
IMPORTING
ep_table = new_table.
Create a new Line with the same structure of the table.
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.
Test it...
DO 30 TIMES.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
<l_field> = sy-index.
INSERT <l_line> INTO TABLE <l_table>.
ENDDO.
LOOP AT <l_table> ASSIGNING <l_line>.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
WRITE <l_field>.
ENDLOOP.
<b>Reward if helpful.</b>
09-03-2007 9:17 AM
Hi,
Thanks for your quick reply but I still have a doubt.
How do I pass name to <l_table>.
for example as shown below how do I make <l_table> as itab while running.
LOOP AT <l_table> ASSIGNING <l_line>.
ENDLOOP.
LOOP AT itab ASSIGNING <l_line>.
ENDLOOP.
09-03-2007 9:26 AM
Hi Apps
1.you need to declare field for dynabic table into GT_FLD(from my example I woud like field GJAHR of table BSID , I will declare like this
REFRESH: GT_FLD.
REFRESH GT_ALV_CAT.
MOVE 'GJAHR' TO GT_FLD. APPEND GT_FLD.
MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.
MOVE 'GJAHR' TO GT_ALV_CAT-REF_FIELD.
MOVE 'GJAHR' TO GT_ALV_CAT-FIELDNAME.
APPEND GT_ALV_CAT.
2. use this below function to create dynamic table .
make dynamic table to use alv method
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING IT_FIELDCATALOG = GT_ALV_CAT[]
IMPORTING EP_TABLE = GD_REF1.
ASSIGN GD_REF1->* TO <GT_1>.
3. you cannot name the dynamic table . if you want to use the dynamic table ,you will use the field symbol(<GT_1>) .
4.if you want to change a table , you will redo the above process again.
Regards
Wiboon
09-03-2007 9:26 AM
hi
good
go through this code
REPORT zpwtest .
Tables
DATA: lt_data TYPE REF TO data.
DATA: lt_fieldcatalog TYPE lvc_t_fcat.
Structure
DATA: ls_fieldcatalog TYPE lvc_s_fcat.
Data References
DATA: new_line TYPE REF TO data.
Field Symbols
FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
<fs_1> TYPE ANY TABLE,
<fs_2>,
<fs_3>.
ls_fieldcatalog-fieldname = 'MANDT'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.
ls_fieldcatalog-fieldname = 'CARRID'. "Fieldname
ls_fieldcatalog-inttype = 'C'. "Internal Type C-> Character
APPEND ls_fieldcatalog TO lt_fieldcatalog.
ls_fieldcatalog-fieldname = 'CONNID'.
ls_fieldcatalog-inttype = 'N'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.
ls_fieldcatalog-fieldname = 'FLDATE'.
ls_fieldcatalog-inttype = 'D'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.
ls_fieldcatalog-fieldname = 'PRICE'.
ls_fieldcatalog-inttype = 'P'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.
ls_fieldcatalog-fieldname = 'CURRENCY'.
ls_fieldcatalog-inttype = 'C'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.
ASSIGN lt_data TO <fs_data>.
CALL METHOD cl_alv_table_create=create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcatalog
IMPORTING
ep_table = fs_data
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2
.
IF sy-subrc <> 0.
ENDIF.
So <FS_1> now points to our dynamic internal table.
ASSIGN <fs_data>->* TO <fs_1>.
Next step is to create a work area for our dynamic internal table.
CREATE DATA new_line LIKE LINE OF <fs_1>.
A field-symbol to access that work area
ASSIGN new_line->* TO <fs_2>.
And to put the data in the internal table
SELECT mandt carrid connid fldate price currency
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE <fs_1>.
Access contents of internal table
LOOP AT <fs_1> ASSIGNING <fs_2>.
ASSIGN COMPONENT 1 OF STRUCTURE <fs_2> TO <fs_3>.
WRITE: / <fs_3>.
ENDLOOP.
thanks
mrutyun^