Application Development 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: 

Internal table Creation dynamically

Former Member
0 Kudos

Please help me to create an internal table dynamically

Thanks,

Ramakrishna

8 REPLIES 8

bpawanchand
Active Contributor
0 Kudos

Hi

[Check this|http://www.sap-img.com/ab030.htm]

[SDN thread|]

Regards

Pavan

former_member188685
Active Contributor
0 Kudos

1.First Get the components from the table(fields) "if you are using the Table

2.Create the fieldcatalog " or else manually create the fieldcat

3.Create the Dynamic Table using the CL_ALV_TABLE_CREATE

4. Now use the select

5. Display the data

Check the sample code.

REPORT  ytest_dynamic.
 
TYPE-POOLS : abap.
DATA : table_des TYPE REF TO cl_abap_structdescr.
DATA : ifields TYPE abap_compdescr_tab,
          wa_field LIKE LINE OF ifields.
DATA: it_fieldcat TYPE lvc_t_fcat,
          wa_fieldcat TYPE lvc_s_fcat.
DATA: i_tab TYPE REF TO data.
 
FIELD-SYMBOLS: <fs> TYPE STANDARD TABLE.
 
PARAMETERS: p_table(30) TYPE c DEFAULT 'SFLIGHT'.
 
 
"Table definiton using the table name
table_des ?= cl_abap_typedescr=>describe_by_name( p_table ).
"Now Read all the fields to a table.
ifields = table_des->components.
 
 
LOOP AT ifields INTO wa_field.
  CLEAR wa_fieldcat.
  wa_fieldcat-fieldname = wa_field-name .
  wa_fieldcat-datatype = wa_field-type_kind.
  wa_fieldcat-inttype = wa_field-type_kind.
  wa_fieldcat-intlen = wa_field-length.
  wa_fieldcat-decimals = wa_field-decimals.
  wa_fieldcat-coltext = wa_field-name.
  wa_fieldcat-outputlen = wa_field-length.
 
  APPEND  wa_fieldcat TO it_fieldcat.
ENDLOOP.
 
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog           = it_fieldcat
  IMPORTING
    ep_table                  = i_tab
*    e_style_fname             =
  EXCEPTIONS
    generate_subpool_dir_full = 1
    OTHERS                    = 2
        .
IF sy-subrc ne  0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
ASSIGN i_tab->* TO <fs>.
 
*-fill the data
APPEND INITIAL LINE TO <fs>.
 
SELECT  *
INTO TABLE <fs>
FROM (p_table)
UP TO 20 ROWS.
 
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    i_callback_program = sy-repid
    it_fieldcat_lvc    = it_fieldcat
  TABLES
    t_outtab           = <fs>
  EXCEPTIONS
    program_error      = 1
    OTHERS             = 2.
IF sy-subrc ne  0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

Former Member
0 Kudos

hi,

REPORT Zgen .

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 .

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

&----


  • text

----


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. "get_structure

&----


*& Form create_dynamic_itab

&----


  • text

----


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. "create_dynamic_itab

&----


*& Form get_data

&----


  • text

----


form get_data.

  • * Select Data from table.

select * into table <dyn_table>

from (p_table).

endform. "get_data

  • Write out data from table.

***

&----


*& Form write_out

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM write_out .

CALL FUNCTION 'WS_DOWNLOAD'

EXPORTING

codepage = 'IBM'

filename = 'C:\Documents and Settings\gp1104\Desktop\TEST\test.txt'

filetype = 'DAT'

TABLES

data_tab = <dyn_table>

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_WRITE_ERROR = 2

INVALID_FILESIZE = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

NO_AUTHORITY = 10

OTHERS = 11.

IF SY-SUBRC <> 0.

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

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

ENDIF.

ENDFORM. " write_out

Rgds,

Paras

Former Member
0 Kudos

See the example program on how to create Dynamic IT :

   
  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.

REPORT ZCLUST1 .
*
* 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. 

Former Member
0 Kudos

refer to the sample code below for dynamic creation of internal table:

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

With luck,

Pritam.

Former Member
0 Kudos

Hi Ramakrishna.

I would like to suggest a couple of references,

[SDN Wiki Code Gallery - Standard Reference for Creating Internal Table dynamically and displaying it as ALV with header and footer text|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/dynamic%2binternal%2btable]

[SDN Weblog Code Gallery - Standard Reference for Dynamic Internal Tables and Structures - ABAP|https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2071] [original link is broken] [original link is broken] [original link is broken];

Hope that's usefull.

Good Luck & Regards.

Harsh Dave

former_member181995
Active Contributor
0 Kudos

RR,

See this:

[Blog of Rich (King Of Abap)|https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2071] [original link is broken] [original link is broken] [original link is broken];

Amit.

ian_maxwell2
Active Participant
0 Kudos

It depends on exactly what you mean by creation of internal tables dynamically. The following example is taken from part of a program that I did for dynamically populating various DB tables using generic internal tables.


Data:
l_UploadData type ref to data.

create data l_UploadData type standard table of (l_TableName).

assign l_UploadData->* to <UploadData>.


CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
EXPORTING
        FILENAME = l_FileName
        HAS_FIELD_SEPARATOR = 'X'
CHANGING
        DATA_TAB = <UploadData>
EXCEPTIONS
        others = 1.

if ( not l_Parameters-Mode-Insert is initial ).
	Insert (l_Parameters-TableName) from table <UploadData>.
elseif ( not l_Parameters-Mode-Update is initial ).
	Update (l_Parameters-TableName) from table <UploadData>.
elseif ( not l_Parameters-Mode-Modify is initial ).
	Modify (l_Parameters-TableName) from table <UploadData>.
elseif ( not l_Parameters-Mode-delete is initial ).
	Delete (l_Parameters-TableName) from table <UploadData>.
endif.

~Ian