‎2007 May 02 8:57 AM
Dear all experts,
I am new to ABAP ( just gone thr. abap/4 in 21 days.)
Can anybody please tell what is dynamic internal table, and what is the way to create it ?
If I have to give table name and some of its fields dynamically, how can i use this concept ?
I<b> have gone through the similar links regarding dynamic internal tables, from this forum, but being an begineer, i was unable to understand.</b>
Can you please help........
any links / study material is also welcomed.
Surely, your help will be rewarded with points.
waiting........
Regards
Vinay
‎2007 May 02 8:59 AM
Hi,
find below an e.g.
**----------------------------------------------------------------------
** 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
‎2007 May 02 8:59 AM
Hi,
find below an e.g.
**----------------------------------------------------------------------
** 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
‎2007 May 02 8:59 AM
Check this link:
http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm
REward if helpful.
Regards,
Ravi
‎2007 May 02 1:28 PM
Dear ravi,
I have gone through some other link , http://72.14.235.104/search?q=cache:gHndF37ttP4J:www.saptechnical.com/Docs/ABAPCodeSampleForALVGridF...%22dynamicinternaltable%22filetype:doc&hl=en&ct=clnk&cd=1&gl=in
I am having 2 small doubts.
<b>1) </b> they have used..
data <b> i_fcat TYPE lvc_t_fcat</b> , & <b>wa_fcat TYPE lvc_s_fcat</b> ,
<b>&</b>
LOOP AT dyntab.
wa_fcat-fieldname = dyntab-fieldname.
wa_fcat-ref_field = dyntab-fieldname.
wa_fcat-ref_table = dyntab-tabname.
APPEND wa_fcat TO i_fcat .
ENDLOOP.
<b>what is difference between using structure lvc_t_fcat, and lvs_s_fcat ?</b>
<b>2) </b> and <b> being an beginner</b>, i am not able to understand how the assignment is happening to newtab field symbol, from following piece of code.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_fcat
IMPORTING
ep_table = dref.
<b>ASSIGN dref->* TO <newtab>.</b>
SELECT * FROM (dyntab-tabname) INTO TABLE <newtab>.
hope you will help...
<b>waiting for your reply.</b>
Thanks & Regards
Vinay
‎2007 May 02 9:04 AM
Hi...
Dynamic internal table is internal table that we create on the fly with flexible column numbers.
For sample code, please look at this <a href="https://wiki.sdn.sap.com/wiki/display/Snippets/DynamicInternalTable">code tutorial</a>. Hopefully it can help you
Regards,
‎2007 May 02 1:34 PM
Hai,
Dynamic internal table allows you to have different formats depending on the dynamic values.Suppose if you want to display any database table content which may have different structres.Then your internal table should be of dynamic internal table.
See the below code which does this.
REPORT Z_DYNAMIC_TABLE.
PARAMETERS : P_TABLE(16) TYPE C.
FIELD-SYMBOLS:
<DYN_TABLE> TYPE STANDARD TABLE,
<DYN_WA>,
<DYN_FIELD>.
DATA NEW_LINE TYPE REF TO DATA.
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>.
CREATE DATA NEW_LINE LIKE LINE OF <T_ITAB>.
ASSIGN NEW_LINE->* TO <DYN_WA>.
Populating Dynamic internal table
SELECT *
FROM (W_TABNAME) UP TO 100 ROWS
INTO TABLE <T_ITAB>.
Displaying dynamic internal table using Grid.
LOOP AT <T_ITAB> 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.
Hope this helps you a lot.
Regards,
Rama chary.Pammi
‎2007 May 02 1:37 PM
‎2007 May 02 1:50 PM
Hi Vinay,
A dynamic internal table is the one which can be created dynamically at the runtime. Now these internal tables doesn't have a predefined structure.
for creating dynamic internal tables : go through link.:
http://www.sap-img.com/ab030.htm.
https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/1011. [original link is broken] [original link is broken] [original link is broken] [original link is broken]
Reward points if helpful.
Regards,
Hemant