‎2008 Aug 13 3:28 PM
Hi friends,
I have found the following for Dynamic internal table creation using oops.
but its showing some error.
can any one correct it and send me back the corect code.
DATA:
it_output type ref to data,
it_fldct type lvc_t_fcat.
field-symbols:
<fs_output> type table,
<output> type any,
<field> type any.
PERFORM fl_defineitab
TABLES it_fldct
USING: 'STATS' space 0 1 'C' 'NUMC', "expand/collapse
'SETID' space 0 90 'L' 'CHAR', "set id
'SETNAME' space 0 24 'L' 'CHAR', "set name
'PARNT' space 0 90 'L' 'CHAR', "parent set id
'ACCOUNTS' space 0 50 'L' 'CHAR', "accounts
'DELET' space 0 1 'L' 'CHAR'. "deletion indicator
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fldct
IMPORTING
ep_table = it_output
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
assign it_output->* to <fs_output>.
assign local copy of initial line of <fs_output> to <output>.
assign component 'SETID' of structure <output> to <field>.
move 'TEST' to <fs_field>.
append <output> to <fs_output>.
loop at <fs_output> to <output>.
assign component 'SETID' of structure <output> to <field>.
write: / <field>.
endloop.
*----
Form fl_defineitab.
*----
Define Internal Table Structure
*----
Parameters:
*----
-- pt_table structure lvc_s_fcat
-> pi_fname type lvc_fname
-> pi_ctext type lvc_txtcol
-> pi_dcmal type decimals
-> pi_outln type lvc_outlen
-> pi_justx type lvc_just
-> pi_dtype type datatype_d
-
FORM fl_defineitab TABLES pt_table STRUCTURE lvc_s_fcat
USING pi_fname
pi_ctext
pi_dcmal
pi_outln
pi_justx
pi_dtype.
local work area
DATA:
lwa_fldct TYPE lvc_s_fcat.
lwa_fldct-fieldname = pi_fname. "fielname
lwa_fldct-coltext = pi_ctext. "field description
lwa_fldct-decimals_o = pi_dcmal. "number of decimal places
lwa_fldct-outputlen = pi_outln. "output length
lwa_fldct-just = pi_justx. "justification
lwa_fldct-datatype = pi_dtype. "data type
APPEND lwa_fldct TO pt_table.
CLEAR lwa_fldct.
ENDFORM. " fl_defineitab
‎2008 Aug 13 3:39 PM
Check this one:
DATA:
it_output type ref to data,
lt_line type ref to data, " <<
it_fldct type lvc_t_fcat.
field-symbols:
<fs_output> type table,
<output> type any,
<field> type any.
PERFORM fl_defineitab
TABLES it_fldct
USING: 'STATS' space 0 1 'C' 'NUMC', "expand/collapse
'SETID' space 0 90 'L' 'CHAR', "set id
'SETNAME' space 0 24 'L' 'CHAR', "set name
'PARNT' space 0 90 'L' 'CHAR', "parent set id
'ACCOUNTS' space 0 50 'L' 'CHAR', "accounts
'DELET' space 0 1 'L' 'CHAR'. "deletion indicator
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fldct
IMPORTING
ep_table = it_output
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
assign it_output->* to <fs_output>.
create data lt_line like line of <fs_output>. " <<
assign lt_line->* to <output>. " <<
assign component 'SETID' of structure <output> to <field>.
move 'TEST' to <field>.
append <output> to <fs_output>.
loop at <fs_output> assigning <output>.
assign component 'SETID' of structure <output> to <field>.
write: / <field>.
endloop.
*--------------------------------------------------------------------------------
FORM fl_defineitab TABLES pt_table STRUCTURE lvc_s_fcat
USING pi_fname
pi_ctext
pi_dcmal
pi_outln
pi_justx
pi_dtype.
*local work area
DATA:
lwa_fldct TYPE lvc_s_fcat.
lwa_fldct-fieldname = pi_fname. "fielname
lwa_fldct-coltext = pi_ctext. "field description
lwa_fldct-decimals_o = pi_dcmal. "number of decimal places
lwa_fldct-outputlen = pi_outln. "output length
lwa_fldct-just = pi_justx. "justification
lwa_fldct-datatype = pi_dtype. "data type
APPEND lwa_fldct TO pt_table.
CLEAR lwa_fldct.
ENDFORM. " fl_defineitab
Regards,
Naimesh Patel
‎2008 Aug 13 3:39 PM
Check this one:
DATA:
it_output type ref to data,
lt_line type ref to data, " <<
it_fldct type lvc_t_fcat.
field-symbols:
<fs_output> type table,
<output> type any,
<field> type any.
PERFORM fl_defineitab
TABLES it_fldct
USING: 'STATS' space 0 1 'C' 'NUMC', "expand/collapse
'SETID' space 0 90 'L' 'CHAR', "set id
'SETNAME' space 0 24 'L' 'CHAR', "set name
'PARNT' space 0 90 'L' 'CHAR', "parent set id
'ACCOUNTS' space 0 50 'L' 'CHAR', "accounts
'DELET' space 0 1 'L' 'CHAR'. "deletion indicator
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fldct
IMPORTING
ep_table = it_output
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
assign it_output->* to <fs_output>.
create data lt_line like line of <fs_output>. " <<
assign lt_line->* to <output>. " <<
assign component 'SETID' of structure <output> to <field>.
move 'TEST' to <field>.
append <output> to <fs_output>.
loop at <fs_output> assigning <output>.
assign component 'SETID' of structure <output> to <field>.
write: / <field>.
endloop.
*--------------------------------------------------------------------------------
FORM fl_defineitab TABLES pt_table STRUCTURE lvc_s_fcat
USING pi_fname
pi_ctext
pi_dcmal
pi_outln
pi_justx
pi_dtype.
*local work area
DATA:
lwa_fldct TYPE lvc_s_fcat.
lwa_fldct-fieldname = pi_fname. "fielname
lwa_fldct-coltext = pi_ctext. "field description
lwa_fldct-decimals_o = pi_dcmal. "number of decimal places
lwa_fldct-outputlen = pi_outln. "output length
lwa_fldct-just = pi_justx. "justification
lwa_fldct-datatype = pi_dtype. "data type
APPEND lwa_fldct TO pt_table.
CLEAR lwa_fldct.
ENDFORM. " fl_defineitab
Regards,
Naimesh Patel
‎2008 Aug 13 3:57 PM
But iam not getting what exactly the code is performing Naimesh.
can you describe me in short.
Regards,
priyanka.
‎2008 Aug 13 4:09 PM
When we creat a dynamic table, it will create a table without work area. So, we need to create a workarea to process the data.
Now, since we are creating the dynamic workarea (step 4), we can't access the fields directly. So, we have to access each field by assigning to some other field symbol (step 5)
I have commented the code:
DATA:
IT_OUTPUT TYPE REF TO DATA,
LT_LINE TYPE REF TO DATA, " <<
IT_FLDCT TYPE LVC_T_FCAT.
FIELD-SYMBOLS:
<FS_OUTPUT> TYPE TABLE,
<OUTPUT> TYPE ANY,
<FIELD> TYPE ANY.
* 1. Fills the Fieldcatalog using the subroutine FL_DEFINEITAB
PERFORM FL_DEFINEITAB
TABLES IT_FLDCT
USING: 'STATS' SPACE 0 1 'C' 'NUMC', "expand/collapse
'SETID' SPACE 0 90 'L' 'CHAR', "set id
'SETNAME' SPACE 0 24 'L' 'CHAR', "set name
'PARNT' SPACE 0 90 'L' 'CHAR', "parent set id
'ACCOUNTS' SPACE 0 50 'L' 'CHAR', "accounts
'DELET' SPACE 0 1 'L' 'CHAR'. "deletion indicator
* 2. Calls the CREATE_DYNAMIC_TABLE method of the class CL_ALV_TABLE_CREATE
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IT_FLDCT
IMPORTING
EP_TABLE = IT_OUTPUT
EXCEPTIONS
GENERATE_SUBPOOL_DIR_FULL = 1
OTHERS = 2.
* 3. Creates a Dynamic Table.
ASSIGN IT_OUTPUT->* TO <FS_OUTPUT>.
* 4. Creates a dynamic work area
CREATE DATA LT_LINE LIKE LINE OF <FS_OUTPUT>. " <<
ASSIGN LT_LINE->* TO <OUTPUT>. " <<
* 5. Accessing Field by field of the work area
ASSIGN COMPONENT 'SETID' OF STRUCTURE <OUTPUT> TO <FIELD>.
MOVE 'TEST' TO <FIELD>.
* 6. Append data to the dynamic table
APPEND <OUTPUT> TO <FS_OUTPUT>.
* 7. write data
LOOP AT <FS_OUTPUT> ASSIGNING <OUTPUT>.
ASSIGN COMPONENT 'SETID' OF STRUCTURE <OUTPUT> TO <FIELD>.
WRITE: / <FIELD>.
ENDLOOP.
*--------------------------------------------------------------------------------
FORM FL_DEFINEITAB TABLES PT_TABLE STRUCTURE LVC_S_FCAT
USING PI_FNAME
PI_CTEXT
PI_DCMAL
PI_OUTLN
PI_JUSTX
PI_DTYPE.
*local work area
DATA:
LWA_FLDCT TYPE LVC_S_FCAT.
LWA_FLDCT-FIELDNAME = PI_FNAME. "fielname
LWA_FLDCT-COLTEXT = PI_CTEXT. "field description
LWA_FLDCT-DECIMALS_O = PI_DCMAL. "number of decimal places
LWA_FLDCT-OUTPUTLEN = PI_OUTLN. "output length
LWA_FLDCT-JUST = PI_JUSTX. "justification
LWA_FLDCT-DATATYPE = PI_DTYPE. "data type
APPEND LWA_FLDCT TO PT_TABLE.
CLEAR LWA_FLDCT.
ENDFORM. " fl_defineitab
Regards,
Naimesh Patel
‎2008 Aug 13 4:15 PM
Naimesh when i executing the program iam getting the output as TEST.
can you differnciate between the normal inter table which we create and the internal table which you created using this code.
so that i can understand clearly.
Regards,
priyanka.
‎2008 Aug 13 4:21 PM
Normal internal table would be easy.
TYPES: BEGIN OF TY_TAB,
SETID TYPE CHAR30,
SETNAME TYPE CHAR50,
END OF TY_TAB.
DATA: IT_TAB TYPE STANDARD TABLE OF TY_TAB, " Internal tab
WA_TAB TYPE TY_TAB. "work area
WA_TAB-SETID = 'TEST'.
APPEND WA_TAB TO IT_TAB.
LOOP AT IT_TAB INTO Wa_TAB.
WRITE: / WA_TAB-SETID.
ENDLOOP.
Regards,
Naimesh Patel
‎2008 Aug 13 5:31 PM
ha what you said is correct.
normal internal table is easy to develop.
then you are you going for such a complex code.
is there any technical difference between those 2 .
Regards,
priyanka
‎2008 Aug 13 5:48 PM
You can use the dynamic internal table when you don't know your name / number of columns when you design the report. It is used, when you will be getting your at runtime.
Regards,
Naimesh Patel
‎2008 Aug 14 6:30 AM
Hello Priyanka
You may have a look at my Wiki posting [Creating Flat and Complex Internal Tables Dynamically using RTTI|https://wiki.sdn.sap.com/wiki/display/Snippets/CreatingFlatandComplexInternalTablesDynamicallyusingRTTI]
Regards
Uwe
‎2008 Aug 14 6:57 AM
hi Priyanka,
Check this code..
PARAMETERS PTAB TYPE DD02L-TABNAME .
FIELD-SYMBOLS: <FS> TYPE ANY TABLE,
<FS_WA> TYPE ANY,
<FS_COMP> TYPE ANY.
DATA: RSTR TYPE REF TO CL_ABAP_STRUCTDESCR,
RTAB TYPE REF TO CL_ABAP_TABLEDESCR,
RGRID TYPE REF TO CL_GUI_ALV_GRID,
RCONT TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
DFIELD TYPE DDFIELDS,
RDATA TYPE REF TO DATA,
WDATA TYPE REF TO DATA,
WA LIKE LINE OF DFIELD,
OK_CODE TYPE SY-UCOMM.
START-OF-SELECTION.
RSTR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( PTAB ).
RTAB = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = RSTR
P_TABLE_KIND = CL_ABAP_TABLEDESCR=>TABLEKIND_STD ).
CREATE DATA RDATA TYPE HANDLE RTAB.
ASSIGN RDATA->* TO <FS>.
CREATE DATA WDATA LIKE LINE OF <FS>.
ASSIGN WDATA->* TO <FS_WA>.
SELECT * FROM (PTAB) INTO TABLE <FS>.
LOOP AT <FS> INTO <FS_WA>.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_WA> TO <FS_COMP>.
IF SY-SUBRC NE 0.
SKIP.
EXIT.
ELSE.
WRITE: <FS_COMP>.
ENDIF.
ENDDO.
ENDLOOP.