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

internal table

Former Member
0 Likes
607

hai,

how to create dymanic internal table?

hazam

6 REPLIES 6
Read only

Former Member
0 Likes
574

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.

Read only

Former Member
0 Likes
574

Hi Hazam,

This question was asked a few times.

Just check these threads:

Regards,

SP.

Read only

Former Member
0 Likes
574

Hi

Take a look at my weblog

/people/thomas.jung3/blog/2005/09/08/oo-abap-dynpro-programming

If you are working 4.7 ir higher, you can do this as well

CREATE DATA mytab TYPE TABLE of (TableName).

Regards,

Ravi

Note :Please close the thread if the issue is resolved

Read only

Former Member
0 Likes
574

Hi,

Refer this code:

  • This program permits to create or update lines from source table

  • to target table with create internal tables dynamically.

  • If we have also the possibilities to include conditions for selecting

  • data with where dynamic and with syntax-check of this conditions .

Code

**----


    • Program name.: ZSELECT_DYNAMIC - MOSHEG - from version 4.6

  • This program permits to create or update lines from source table

  • to target table with create internal tables dynamically.

  • If we have also the possibilities to include conditions for selecting

  • data with where dynamic and with syntax-check of this conditions .

*----


  • parameters for this program :

*----


  • Source table Z?????

  • Target table Z?????

  • _ client speciifed

*

  • Code line1 for where dynamic

  • line2 for where dynamic

  • line3 for where dynamic

  • line4 for where dynamic

*----


REPORT zselect_dynamic LINE-SIZE 132

LINE-COUNT 65(1)

NO STANDARD PAGE HEADING

MESSAGE-ID z1.

TYPES ztab LIKE dcobjdef-name .

PARAMETERS: tab_name TYPE ztab DEFAULT 'Z?????' ,

tab_nam2 TYPE ztab DEFAULT 'Z?????' ,

pclient AS CHECKBOX .

SELECTION-SCREEN SKIP .

PARAMETERS: where1(80) ,

where2(80) ,

where3(80) ,

where4(80) .

*

DATA : lcode(72),

prog_tab LIKE lcode OCCURS 0 WITH HEADER LINE .

DEFINE append_line.

append &1 to prog_tab.

END-OF-DEFINITION.

DATA: BEGIN OF nametab OCCURS 0.

INCLUDE STRUCTURE dntab.

DATA: END OF nametab.

DATA: BEGIN OF twhere OCCURS 20,

line(80),

END OF twhere.

DATA: zprogram LIKE sy-cprog,

no_line TYPE i,

zmessage(150) ,

count_commit TYPE i .

DATA: d_ref TYPE REF TO data,

d_ref2 TYPE REF TO data ,

lt_alv_cat TYPE TABLE OF lvc_s_fcat,

ls_alv_cat LIKE LINE OF lt_alv_cat.

FIELD-SYMBOLS : TYPE table,

,

,"TYPE ANY ,

,

.

**----


    • Main program.

**----


START-OF-SELECTION.

END-OF-SELECTION.

**----


    • Main program.

**----


PERFORM z_define_itab .

&----


*& Form z_define_itab

&----


FORM z_define_itab .

CHECK ( tab_name(01) = 'Z' OR tab_name(01) = 'Y' )

  • if you want treat tables with your namespace, insert here your code

AND ( tab_nam2(01) = 'Z' OR tab_nam2(01) = 'Y' ) .

REFRESH nametab.

CALL FUNCTION 'NAMETAB_GET'

EXPORTING

langu = sy-langu

tabname = tab_name

TABLES

nametab = nametab

EXCEPTIONS

no_texts_found = 1.

LOOP AT nametab .

ls_alv_cat-fieldname = nametab-fieldname .

ls_alv_cat-ref_table = tab_name.

ls_alv_cat-ref_field = nametab-fieldname .

APPEND ls_alv_cat TO lt_alv_cat.

ENDLOOP.

  • internal table build

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_alv_cat

IMPORTING ep_table = d_ref .

ASSIGN d_ref->* TO .

IF where1 IS INITIAL AND where2 IS INITIAL

AND where3 IS INITIAL AND where4 IS INITIAL .

SELECT * FROM (tab_name) INTO TABLE .

  • ORDER BY PRIMARY KEY.

ELSE .

PERFORM select_with_where .

ENDIF .

DESCRIBE TABLE LINES sy-tfill .

IF sy-tfill = 0 .

MESSAGE i000 WITH

'Data not selected, verify the tables or conditions ! ' .

STOP .

ELSE .

MESSAGE s000 WITH 'You have successfully treated yours tables.'.

ENDIF .

CREATE DATA d_ref2 TYPE (tab_nam2).

ASSIGN d_ref2->* TO .

LOOP AT ASSIGNING .

CLEAR .

LOOP AT nametab .

ASSIGN COMPONENT nametab-fieldname

OF STRUCTURE TO .

IF sy-subrc <> 0. EXIT. ENDIF.

ASSIGN COMPONENT nametab-fieldname OF STRUCTURE TO .

IF sy-subrc = 0.

= .

ENDIF.

ENDLOOP .

CHECK sy-subrc = 0.

INSERT INTO (tab_nam2) VALUES .

IF sy-subrc NE 0 .

UPDATE (tab_nam2) FROM .

ENDIF .

ADD 1 TO count_commit .

IF count_commit = '10000' .

COMMIT WORK .

count_commit = 0 .

ENDIF .

WRITE : .

ENDLOOP .

ENDFORM. " z_define_itab

&----


*& Form select_with_where

&----


FORM select_with_where.

IF NOT where1 IS INITIAL .

twhere-line = where1 . APPEND twhere .

ENDIF .

IF NOT where2 IS INITIAL .

twhere-line = where2 . APPEND twhere .

ENDIF .

IF NOT where3 IS INITIAL .

twhere-line = where3 . APPEND twhere .

ENDIF .

IF NOT where4 IS INITIAL .

twhere-line = where4 . APPEND twhere .

ENDIF .

REFRESH prog_tab.

append_line 'REPORT ZGEN .'.

append_line 'TABLES:'.

append_line tab_name.

append_line '.'.

append_line 'DATA: ITAB LIKE'.

append_line tab_name.

append_line 'OCCURS 0 WITH HEADER LINE.'.

append_line 'FORM SELECT_TABLE.'.

append_line 'SELECT * FROM'.

append_line tab_name.

IF pclient = 'X'.

append_line 'CLIENT SPECIFIED'.

ENDIF.

append_line 'INTO TABLE ITAB'.

append_line 'WHERE '.

LOOP AT twhere.

append_line twhere.

ENDLOOP.

append_line ' . '.

append_line 'ENDFORM.'.

PERFORM generate_form .

ENDFORM. " select_with_where

&----


*& Form generate_form

&----


FORM generate_form .

GENERATE SUBROUTINE POOL prog_tab NAME zprogram

MESSAGE zmessage LINE no_line .

IF sy-subrc NE 0.

WRITE: / 'Syntax error : ', zmessage,

/ 'in line', no_line .

STOP.

ENDIF.

SELECT * FROM (tab_name) INTO TABLE

WHERE (twhere) .

ENDFORM. " generate_form

rgds,

Latheesh.

Read only

vinod_gunaware2
Active Contributor
Read only

Former Member
0 Likes
574
******DATA DECLARATION*****************************

FIELD-SYMBOLS : <it_final> TYPE STANDARD TABLE,
                <wa_final> TYPE ANY,
                <w_field> TYPE ANY.


***DYNAMIC CREATION OF FIELDCATALOG****************

*FIRST 2 FIELDS FIELDS FIELD1 AND FIELD2 ARE CONSTANT, FIELDS OBTAINED IN THE LOOP ENDLOOP ARE DYNAMIC,
*LIKEWISE DYNAMIC FIELDCATALOG IS CREATED

  wa_fieldcatalog-fieldname  = 'FIELD1'.
  wa_fieldcatalog-ref_table  = 'E070'.
  wa_fieldcatalog-outputlen  = '13'.
  wa_fieldcatalog-reptext    = 'Created On'.
  wa_fieldcatalog-seltext    = 'Created On'.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
  CLEAR wa_fieldcatalog.
  
  wa_fieldcatalog-fieldname  = 'FIELD1'.
  wa_fieldcatalog-ref_table  = 'E070'.
  wa_fieldcatalog-outputlen  = '13'.
  wa_fieldcatalog-reptext    = 'Created On'.
  wa_fieldcatalog-seltext    = 'Created On'.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
  CLEAR wa_fieldcatalog.


  LOOP AT it_mandt WHERE mandt IN s_mandt.
    CONCATENATE 'CLNT' it_mandt INTO wa_fieldcatalog-fieldname.
    wa_fieldcatalog-inttype    = 'NUMC'.
    wa_fieldcatalog-outputlen  = '14'.
    wa_fieldcatalog-reptext    = it_mandt.
    wa_fieldcatalog-seltext    = it_mandt.

    APPEND wa_fieldcatalog TO it_fieldcatalog.
    CLEAR :wa_fieldcatalog ,it_mandt.
  ENDLOOP.


********CREATE DYNAMIC TABLE************************

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = it_fieldcatalog
    IMPORTING
      ep_table                  = new_table
    EXCEPTIONS
      generate_subpool_dir_full = 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.

  ASSIGN new_table->* TO <it_final>.

*********CREATE WORK AREA****************************

CREATE DATA new_line LIKE LINE OF <it_final>.
  ASSIGN new_line->* TO <wa_final>.

*********INSERTTING WORK AREAR TO INTERNAL TABLE******

    INSERT <wa_final> INTO TABLE <it_final>.

*******POPULATING DATA*******************************  
  LOOP.
   
   ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_final> TO <w_field>.
   <w_field> = '12345'.

    ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_final> TO <w_field>.
   <w_field> = '21453DD'.

   FIELD1 AND FIELD2 ARE COMPONENTS OF FIELDCATALOG.
    
ENDLOOP.      

  ENDLOOP.