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

Dynamic internal table creation using oops.

Former Member
0 Likes
1,356

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

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
1,092

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

9 REPLIES 9
Read only

naimesh_patel
Active Contributor
0 Likes
1,093

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

Read only

0 Likes
1,092

But iam not getting what exactly the code is performing Naimesh.

can you describe me in short.

Regards,

priyanka.

Read only

0 Likes
1,092

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

Read only

0 Likes
1,092

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.

Read only

0 Likes
1,092

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

Read only

0 Likes
1,092

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

Read only

0 Likes
1,092

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

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,092

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

Read only

Former Member
0 Likes
1,092

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.