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

Former Member
0 Likes
1,080

I wish to select all the condintions from the database......and use the dynamic internal table to display the output..........

How t do the same.Please give some example to explain

1 ACCEPTED SOLUTION
Read only

prasanth_kasturi
Active Contributor
0 Likes
1,001

hi,

Yes , you can create a Dynamic Internal table .Just chek out this program .

type-pools : abap.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

Write out data from table.

loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

http://www.sap-img.com/ab030.htm

This is a very simple tip to create a dynamic internal table, ie. a table not defined until runtime, inside ABAP code.

OK, so it only references an SAP database table, but this should be good enough for most applications.

Code

There are a few declarations to make:

field-symbols: <table> type any.

types: fieldref type ref to data.

data: dyn_table type fieldref.

As for the actual code to generate the table:

create data dyn_table type (SAP Table).

assign dyn_table->* to table.

The table is now a field symbol which can be referenced in the normal way.

regards

prasanth

9 REPLIES 9
Read only

prasanth_kasturi
Active Contributor
0 Likes
1,002

hi,

Yes , you can create a Dynamic Internal table .Just chek out this program .

type-pools : abap.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

Write out data from table.

loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

http://www.sap-img.com/ab030.htm

This is a very simple tip to create a dynamic internal table, ie. a table not defined until runtime, inside ABAP code.

OK, so it only references an SAP database table, but this should be good enough for most applications.

Code

There are a few declarations to make:

field-symbols: <table> type any.

types: fieldref type ref to data.

data: dyn_table type fieldref.

As for the actual code to generate the table:

create data dyn_table type (SAP Table).

assign dyn_table->* to table.

The table is now a field symbol which can be referenced in the normal way.

regards

prasanth

Read only

Former Member
0 Likes
1,001

Hi,

Check this out,

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

Thanks,

Muthu

Read only

Former Member
0 Likes
1,001

This message was moderated.

Read only

0 Likes
1,001

Hi Swastik,

the sample program in your response is fabulous!!

Of course it is just a straight copy and paste of the example from Rich Hielman's blog mentioned in an earlier response.

I think it's great that you can provide a solution on SDN, but not acknowledging the source - and thereby giving the impression that it was all your work - is being less than honest.

In fact the word is plagiarism!!

How about being as generous with information as the people who provided it to you - by acknowledging them!

Interestingly the only original work in your response is the last sentence. The one that reads "Reward points if helpful". Don't you think that should have said "Reward Rich Hielman if helpful"?

Cheers

Graham Robbo

Read only

0 Likes
1,001

None of these code snippets here were created by the posters, I dare say.

Plus, "how to create a dynamic table" has been asked so many times, I will never understand why people can't search the forums.

Good night

Read only

Former Member
0 Likes
1,001

Hi,

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data,

GP_WA TYPE REF TO DATA.

FIELD-SYMBOLS: <GT_WA> TYPE ANY, <gt_table> TYPE table.

DATA: X1 LIKE X030L,

GT_TABLE1 LIKE DNTAB OCCURS 0 WITH HEADER LINE,

HEAD LIKE X030L OCCURS 0 WITH HEADER LINE,

IND1 LIKE SY-TABIX,

IT LIKE MARA OCCURS 10 WITH HEADER LINE.

SELECT * FROM MARA INTO TABLE IT UP TO 30 ROWS.

CALL FUNCTION 'NAMETAB_GET'

EXPORTING

  • LANGU = SY-LANGU

  • ONLY = ' '

TABNAME = 'MARA'

  • IMPORTING

  • HEADER =

  • RC =

TABLES

NAMETAB = GT_TABLE1

  • EXCEPTIONS

  • INTERNAL_ERROR = 1

  • TABLE_HAS_NO_FIELDS = 2

  • TABLE_NOT_ACTIV = 3

  • NO_TEXTS_FOUND = 4

  • OTHERS = 5

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

LOOP AT GT_TABLE1.

IND1 = SY-TABIX.

ENDLOOP.

DO IND1 TIMES.

v_fieldname = 'TEXT' .

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 40.

wa_it_fldcat-intlen = 40.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

DATA: WA_GT LIKE LINE OF GT_TABLE1.

FIELD-SYMBOLS: <FS1>.

CREATE DATA GP_WA LIKE LINE OF <GT_TABLE>.

ASSIGN GP_WA->* TO <GT_WA>.

LOOP AT GT_TABLE1 INTO WA_GT.

ASSIGN COMPONENT SY-TABIX OF STRUCTURE <GT_WA> TO <FS1>.

<FS1> = WA_GT-FIELDTEXT.

ENDLOOP.

APPEND <GT_WA> TO <GT_TABLE>.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

FILENAME = 'C:\WINDOWS\Desktop\T1.XLS'

  • FILETYPE = 'ASC'

  • APPEND = ' '

WRITE_FIELD_SEPARATOR = 'X'

  • HEADER = '00'

  • TRUNC_TRAILING_BLANKS = ' '

  • WRITE_LF = 'X'

  • COL_SELECT = ' '

  • COL_SELECT_MASK = ' '

  • DAT_MODE = ' '

  • CONFIRM_OVERWRITE = ' '

  • NO_AUTH_CHECK = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • WRITE_BOM = ' '

  • TRUNC_TRAILING_BLANKS_EOL = 'X'

  • WK1_N_FORMAT = ' '

  • WK1_N_SIZE = ' '

  • WK1_T_FORMAT = ' '

  • WK1_T_SIZE = ' '

  • WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE

  • SHOW_TRANSFER_STATUS = ABAP_TRUE

  • IMPORTING

  • FILELENGTH =

TABLES

DATA_TAB = <GT_TABLE>

  • FIELDNAMES =

  • EXCEPTIONS

  • FILE_WRITE_ERROR = 1

  • NO_BATCH = 2

  • GUI_REFUSE_FILETRANSFER = 3

  • INVALID_TYPE = 4

  • NO_AUTHORITY = 5

  • UNKNOWN_ERROR = 6

  • HEADER_NOT_ALLOWED = 7

  • SEPARATOR_NOT_ALLOWED = 8

  • FILESIZE_NOT_ALLOWED = 9

  • HEADER_TOO_LONG = 10

  • DP_ERROR_CREATE = 11

  • DP_ERROR_SEND = 12

  • DP_ERROR_WRITE = 13

  • UNKNOWN_DP_ERROR = 14

  • ACCESS_DENIED = 15

  • DP_OUT_OF_MEMORY = 16

  • DISK_FULL = 17

  • DP_TIMEOUT = 18

  • FILE_NOT_FOUND = 19

  • DATAPROVIDER_EXCEPTION = 20

  • CONTROL_FLUSH_ERROR = 21

  • OTHERS = 22

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Thanks,

Arunprasad.P

Read only

Former Member
0 Likes
1,001

Dynamic internal table is an extension to internal table concept, used when the number of fields is not known at the design time or until the compile time.

Scenario 1:

Display the table of contents in grid format using the dynamic internal table.

Creating Dynamic internal table

PARAMETERS : p_table(10) TYPE C.

DATA: w_tabname TYPE w_tabname,

w_dref TYPE REF TO data,

w_grid TYPE REF TO cl_gui_alv_grid.

FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE.

w_tabname = p_table.

CREATE DATA w_dref TYPE TABLE OF (w_tabname).

ASSIGN w_dref->* TO <t_itab>.

Populating Dynamic internal table

SELECT *

FROM (w_tabname) UP TO 20 ROWS

INTO TABLE <t_itab>.

Displaying dynamic internal table using Grid.

CREATE OBJECT w_grid

EXPORTING i_parent = cl_gui_container=>screen0.

CALL METHOD w_grid->set_table_for_first_display

EXPORTING

i_structure_name = w_tabname

CHANGING

it_outtab = <t_itab>.

CALL SCREEN 100.

Scenario 2:

Create a dynamic internal table with the specified number of columns.

Creating Dynamic internal table

TYPE-POOLS: slis.

FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE, u201C Dynamic internal table name

<fs_dyntable>, u201C Field symbol to create work area

<fs_fldval> type any. u201C Field symbol to assign values

PARAMETERS: p_cols(5) TYPE c. u201C Input number of columns

DATA: t_newtable TYPE REF TO data,

t_newline TYPE REF TO data,

t_fldcat TYPE slis_t_fldcat_alv,

t_fldcat TYPE lvc_t_fcat,

wa_it_fldcat TYPE lvc_s_fcat,

wa_colno(2) TYPE n,

wa_flname(5) TYPE c.

  • Create fields .

DO p_cols TIMES.

CLEAR wa_it_fldcat.

move sy-index to wa_colno.

concatenate 'COL'

wa_colno

into wa_flname.

wa_it_fldcat-fieldname = wa_flname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 10.

APPEND wa_it_fldcat TO t_fldcat.

ENDDO.

  • Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fldcat

IMPORTING

ep_table = t_newtable.

ASSIGN t_newtable->* TO <t_dyntable>.

  • Create dynamic work area and assign to FS

CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

ASSIGN t_newline->* TO <fs_dyntable>.

Populating Dynamic internal table

DATA: fieldname(20) TYPE c.

DATA: fieldvalue(10) TYPE c.

DATA: index(3) TYPE c.

DO p_cols TIMES.

index = sy-index.

MOVE sy-index TO wa_colno.

CONCATENATE 'COL'

wa_colno

INTO wa_flname.

  • Set up fieldvalue

CONCATENATE 'VALUE' index INTO

fieldvalue.

CONDENSE fieldvalue NO-GAPS.

ASSIGN COMPONENT wa_flname

OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

<fs_fldval> = fieldvalue.

ENDDO.

  • Append to the dynamic internal table

APPEND <fs_dyntable> TO <t_dyntable>.

Displaying dynamic internal table using Grid.

DATA: wa_cat LIKE LINE OF fs_fldcat.

DO p_cols TIMES.

CLEAR wa_cat.

MOVE sy-index TO wa_colno.

CONCATENATE 'COL'

wa_colno

INTO wa_flname.

wa_cat-fieldname = wa_flname.

wa_cat-seltext_s = wa_flname.

wa_cat-outputlen = '10'.

APPEND wa_cat TO fs_fldcat.

ENDDO.

  • Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

it_fieldcat = fs_fldcat

TABLES

t_outtab = <t_dyntable>.

hope this may be helpful....

regards...

praveena.

Read only

Former Member
0 Likes
1,001

hi,

  • Example: how to create a dynamic internal table

  • The dynamic internal table stucture

DATA: BEGIN OF STRUCT OCCURS 10,

FILDNAME(8) TYPE C,

ABPTYPE TYPE C,

LENGTH TYPE I,

END OF STRUCT.

  • The dynamic program source table

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

  • Sample dynamic internal table stucture

STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

APPEND STRUCT. CLEAR STRUCT.

  • Create the dynamic internal table definition in the dyn. program

INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.

INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.

INCTABL-LINE = STRUCT-FILDNAME.

LNG = STRLEN( STRUCT-FILDNAME ).

IF NOT STRUCT-LENGTH IS INITIAL .

TYPESRTING(1) = '('.

TYPESRTING+1 = STRUCT-LENGTH.

TYPESRTING+5 = ')'.

CONDENSE TYPESRTING NO-GAPS.

INCTABL-LINE+LNG = TYPESRTING.

ENDIF.

INCTABL-LINE+15 = 'type '.

INCTABL-LINE+21 = STRUCT-ABPTYPE.

INCTABL-LINE+22 = ','.

APPEND INCTABL.

ENDLOOP.

INCTABL-LINE = 'end of dyntab. '.

APPEND INCTABL.

  • Create the code processes the dynamic internal table

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.

INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'endloop.'. APPEND INCTABL.

  • Create and run the dynamic program

INSERT REPORT 'zdynpro'(001) FROM INCTABL.

SUBMIT ZDYNPRO.

-


or Just try out this simpler dynamic internal tables

DATA: itab TYPE STANDARD TABLE OF spfli,

wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

list LIKE TABLE OF line(72).

START-OF-SELECTION.

*line = ' CITYFROM CITYTO '.

line = ' AIRPTO '.

APPEND line TO list.

SELECT DISTINCT (list)

INTO CORRESPONDING FIELDS OF TABLE itab

FROM spfli.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa.

  • WRITE: / wa-cityfrom, wa-cityto.

WRITE 😕 wa-airpto.

ENDLOOP.

ENDIF.

hope this might be useful..

regards,

praveena.

Read only

Former Member
0 Likes
1,001

report  ztest_sourav10.

type-pools: abap, slis.

data: tab type ref to cl_abap_structdescr,
      wa_tab type ref to cl_abap_structdescr,
      comp_tab   type cl_abap_structdescr=>component_table,
      comp like line of comp_tab,
      i_tab type ref to cl_abap_tabledescr,
      i_table type ref to data.

data:
l_program_name   type syrepid value sy-repid,
l_structure_name type tabname,
l_i_ct_fieldcat  type slis_t_fieldcat_alv.


field-symbols:
<f_tab1> type standard table.

parameters:
p_tname type tabname16 obligatory, " DEFAULT 'MARA' ,
p_rows(5) type c default '200'.

tab ?= cl_abap_typedescr=>describe_by_name( p_tname ).
comp_tab = tab->get_components( ).
wa_tab  = cl_abap_structdescr=>create( comp_tab ).
i_tab   = cl_abap_tabledescr=>create( wa_tab ).
create data i_table type handle i_tab.
*create data wa_table type handle wa_tab.
assign i_table->* to <f_tab1>.
*assign wa_table->* to <f1>.

if p_rows is initial.
  p_rows = '50000'.
endif.

at selection-screen.

  select count( * )
    from dd02l
    where tabname  = p_tname
    and   as4local = 'A'
    and   tabclass = 'TRANSP'.
  if sy-subrc <> 0.
    message e000(z_zzz_ca_messages) with
    p_tname 'is not a Transparant Table'.
  endif.




start-of-selection.
*Get data
  select * from (p_tname)
  into table <f_tab1>
  up to p_rows rows.

  if sy-subrc <> 0.
    message i000(z_zzz_ca_messages) with 'No data found'.
    leave list-processing.
  endif.

end-of-selection.

  set titlebar sy-title
  of program sy-cprog
  with 'Display table:'   p_tname.

  l_structure_name = p_tname.

  call function 'REUSE_ALV_FIELDCATALOG_MERGE'
    exporting
      i_program_name               = l_program_name
      i_internal_tabname           = 'I_TABLE'
      i_structure_name             = l_structure_name
      i_client_never_display       = 'X'
*   I_INCLNAME                   =
*   I_BYPASSING_BUFFER           =
*   I_BUFFER_ACTIVE              =
    changing
      ct_fieldcat                  = l_i_ct_fieldcat
   exceptions
     inconsistent_interface       = 1
     program_error                = 2
     others                       = 3
            .
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.

    call function 'REUSE_ALV_GRID_DISPLAY'
      exporting
        i_callback_program = l_program_name
        it_fieldcat        = l_i_ct_fieldcat
      tables
        t_outtab           = <f_tab1>
      exceptions
        program_error      = 1
        others             = 2.
    if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
              with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.
  endif.