‎2006 Jun 08 10:54 AM
Hi,
i have a requirement to create two internal table of same data type, the input string will be table name say "MARA"
now i have create dynamically the two internal table like as below
data : itab1 like mara occurs 0 with header line.
data : itab2 like mara occurs 0 with header line.
now can you please tell me how to create these above two internal table on fly.
thanks,
John.
‎2006 Jun 08 10:57 AM
Hi,
refr this code:
Create dynamic table with conditions
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
Message was edited by: Latheesh Kaduthara
‎2006 Jun 08 10:57 AM
table_name = <Table name>.
CREATE DATA my_table TYPE STANDARD TABLE OF (table_name).
Regards,
Ravi
‎2006 Jun 08 10:58 AM
‎2006 Jun 08 10:59 AM
Hi John,
Accpet table name and
declare your interntal tables as follows.
data: tabname like DD02L-TABNAME value 'MARA'.
data: <b>itab1 like (tabname) occurs 0 with header line,
itab2 like (tabname) occurs 0 with header line.</b>
Based on the value of tabname the internal tables will be created.
Thanks,
Vinay
‎2006 Jun 08 11:05 AM
/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table
Refer the above link which is web blog about Dynamic Internal Table already availiable on SAP SDN site...
Please award points if found helpful
‎2006 Jun 08 11:06 AM
hi,
Check this ; very good blog on SDN for The Same.
<b>Dynamic Internal table -weblog in sdn</b>
/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table
<b>Mark Helpful Answers</b>
Regards
‎2006 Jun 08 11:09 AM
PARAMETERS pa_dbtab TYPE dd02l-tabname DEFAULT 'SFLIGHT'.
DATA d_ref TYPE REF TO data.
FIELD-SYMBOLS: <fs_wa> TYPE ANY, <fs_comp> TYPE ANY.
CREATE DATA d_ref TYPE (pa_dbtab).
ASSIGN d_ref->* TO <fs_wa>.
SELECT * FROM (pa_dbtab) INTO <fs_wa>.
Regards,
Wasim Ahmed
‎2006 Jun 08 11:18 AM
Hi Wasim,
Refer the code below :
report ZDSAP .
data: d_ref type ref to data,
d_ref2 type ref to data ,
i_alv_cat type table of lvc_s_fcat,
ls_alv_cat like line of i_alv_cat.
types tabname like dcobjdef-name .
parameter: p_tablen type tabname.
data: begin of itab occurs 0.
include structure dntab.
data: end of itab.
field-symbols : <f_fs> type table,
<f_fs1> type table,
<f_fs2> type any,
<f_fs3> type table,
<f_fs4> type any.
refresh itab.
call function 'NAMETAB_GET'
exporting
langu = sy-langu
tabname = p_tablen
tables
nametab = itab
exceptions
no_texts_found = 1.
loop at itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
append ls_alv_cat to i_alv_cat.
endloop.
* internal table build
call method cl_alv_table_create=>create_dynamic_table
exporting it_fieldcatalog = i_alv_cat
importing ep_table = d_ref .
assign d_ref->* to <f_fs>.
select * from (p_tablen) into corresponding fields of table <f_fs>.
loop at <f_fs> assigning <f_fs2>.
*your code goes here.
assign itab-fieldname to <f_fs4>.
loop at itab.
write: / <f_fs2>-<f_fs4>.
endloop.
endloop.<b>Reward points if it helps.</b>
‎2006 Jun 08 11:22 AM
Hi john,
<b>the input string will be table name say "MARA"</b>
1.
For this purpose,
in my program,
there is an INDEPENDENT FORM
whose inputs are
TABLE NAME / STRUCTURE NAME
and from those, it consructs dynamic table.
2. Here is the program.
the dynamic table name will be
<DYNTABLE>.
3. U can use this program (FORM in this program)
to generate any kind of internal table
by specifying TABLE NAME .
4.
REPORT abc.
*----
COMPULSORY
FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.
FIELD-SYMBOLS: <dynline> TYPE ANY.
DATA: lt TYPE lvc_t_fcat.
DATA: ls TYPE lvc_s_fcat.
FIELD-SYMBOLS: <fld> TYPE ANY.
DATA : fldname(50) TYPE c.
*----
parameters : iname LIKE dd02l-tabname.
*----
START-OF-SELECTION.
*----
PERFORM
PERFORM mydyntable USING lt.
BREAK-POINT.
*----
INDEPENDENT FORM
*----
FORM mydyntable USING ptabname.
*----
Create Dyn Table From FC
FIELD-SYMBOLS: <fs_data> TYPE REF TO data.
FIELD-SYMBOLS: <fs_1>.
FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.
DATA: lt_data TYPE REF TO data.
data : lt TYPE lvc_t_fcat .
DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.
*----
CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'
EXPORTING
tabname = iname
TABLES
ddfields = ddfields.
.
*----
CONSTRUCT FIELD LIST
LOOP AT ddfields.
ls-fieldname = ddfields-fieldname.
APPEND ls TO lt.
ENDLOOP.
ASSIGN lt_data TO <fs_data>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt
IMPORTING
ep_table = <fs_data>
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
*----
Assign Dyn Table To Field Sumbol
ASSIGN <fs_data>->* TO <fs_1>.
ASSIGN <fs_1> TO <fs_2>.
ASSIGN <fs_1> TO <dyntable>.
ENDFORM. "MYDYNTABLE
regards,
amit m.