Page 1 of 2

How to declare an Internal table dynamically?

PostPosted: Sun Apr 27, 2003 10:18 pm
by nazgul
Hello all,

Does anyone know how to declare an internal table dynamically in a program? I need to create a program that will accept from the user (parameter) the name of a Custom Transparent Table. The program would then create an Internal Table having the structure of the Transparent Table specified by the user. I tried using macros but to no avail. The program would look something like this:

parameter: p_tablename type tabname.

data: begin of itab.
include structure p_tablename.
data: end of itab.

Unfortunately this code is not syntactically correct.
Thanks in advance for your time :).

PostPosted: Sun Apr 27, 2003 10:25 pm
by Vijayendra
Hi,

hey its very difficult creating a internal table dynamically.
I faced the same problem but could not figure out how to go about.

have you checked usinf field symbols.

even i am looking for a good solution for this.

regards
Vijayendra

PostPosted: Mon Apr 28, 2003 12:50 am
by projecttoday
Since you are using custom tables, it's possible that there aren't that many. So, you could define your internal tables explicitly for all of these custom tables. That way there would be an internal table available no matter what the user enters.

Note: dynamic table selection has been discussed here many times before. I don't think anyone has yet come up with a way for 100 % dynamic table selection with individual field / structure access for any and all possible tables.

Regards,
Robert Crouser

PostPosted: Mon Apr 28, 2003 3:14 am
by kumar
Read the Manual for the Syntax

GENERATE SUBROUTINE POOL CODE NAME PROG

It is possible to create Dynmic Internal Tables.

KRK

code ....

PostPosted: Mon Apr 28, 2003 3:54 am
by anandksap
hi nazgul,
here is the code ..i've written it for you ...nazgul...just insert the code you wanna do with the content in the field string <f_fs2>.

all the best,
Kathuroju Anand


*&---------------------------------------------------------------------*
*& Report ZDSAP *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

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.

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.
ENDLOOP.

Use the SEARCH facility.

PostPosted: Mon Apr 28, 2003 6:09 am
by Dr Sidewalk
This question must get asked at least once a week!!!

PostPosted: Sun May 04, 2003 9:57 pm
by nazgul
Thank you for all the replies (Vijayendra, projecttoday, kumar, anandksap, Dr Sidewalk). Sorry for my late message, I was only able to view your replies just now. Thanks anandksap for the code. Dr. Sidewalk, I have seen the same topic in the forums before but I didn't realize that the topic appeared that often :D Thanks again!

PostPosted: Mon May 05, 2003 1:55 am
by nazgul
Thanks anandksap. Your code worked just fine. However, I still have a problem accessing the fields of my internal table. If I try to display the contents of <f_fs> by command WRITE, the table entries will appear side by side. In your code, itab contains the field names of the table name. I just need to access these field names found in <f_fs>. Is there a way to do this? I added some code to the program you sent, what I want to do is something like that. Thanks in advance! :)


*&---------------------------------------------------------------------*
*& Report ZDSAP *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
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.

This works to

PostPosted: Tue Apr 13, 2004 6:32 am
by dalzio
Code: Select all
method GET_ITABLE_OF_TYPE_NAME .
*IMPORTING IM_ITABLE_TYPE_NAME TYPE STRING
*RETURNING RE_ITABLE_REF_TO_DATA type ref to data

DATA:
  alv_field_catalog TYPE lvc_t_fcat,
  alv_field LIKE LINE OF alv_field_catalog,
  curr_col_pos LIKE alv_field-col_pos,
  tmp_itable_type_name TYPE DD02L-TABNAME.
curr_col_pos = 1.
*the interface of function "LVC_FIELDCATALOG_MERGE" expect the name with this type
tmp_itable_type_name = im_itable_type_name.

*creating fieldcatalog
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  EXPORTING
    i_structure_name = tmp_itable_type_name
  CHANGING
    ct_fieldcat      = alv_field_catalog
  EXCEPTIONS
    inconsistent_interface = 1
    program_error          = 2
    OTHERS                 = 3.
IF sy-subrc <> 0.
*here jour error handling
ENDIF.


CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog = alv_field_catalog
  IMPORTING
    ep_table = RE_ITABLE_REF_TO_DATA.
*Now ist in re_itable_ref_to_data a pointer to a internal table with the *wished type
endmethod.


To call the method:

Code: Select all
DATA:
table_type_name type String,
ref_to_internal_table type ref to data.
FIELD-SYMBOLS : <internal_Table> type standard table.
table_type_name = 'LIKP'.
CALL METHOD ZCL_MYCLASS=>GET_ITABLE_OF_TYPE_NAME
      EXPORTING
              im_itable_type_name   = table_type_name
            receiving
              re_itable_ref_to_data = ref_to_internal_table.
ASSIGN ref_to_internal_table->* to <internal_Table>.
SELECT * FROM (table_type_name) INTO CORRESPONDING FIELDS OF TABLE <internal_Table>.
etc. etc.


How to declare an Internal table dynamically?

PostPosted: Tue Feb 01, 2005 2:22 am
by sk_pathare
Hello all,

I m new to this club, while searching thru internet i found this code on one site which might help u all. It is totally dynamic internal table creation.

**
* Example: how to create a dynamic internal table
*
* The dynamic internal table stucture
REPORT ZDYNINTTAB.

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: / ''Sdyntab''.'. APPEND INCTABL.
INCTABL-LINE = 'endloop.'. APPEND INCTABL.

* Create and run the dynamic program
INSERT REPORT 'zdynpro'(001) FROM INCTABL.
SUBMIT ZDYNPRO AND RETURN.


LOOP AT DYNTAB.
BREAK-POINT.

ENDLOOP.
WRITE : /.

PostPosted: Tue Feb 01, 2005 3:38 am
by Rich
Ummm... The answer is a click away in the Abappers Knowledge Corner.

Stefan90

PostPosted: Thu Feb 17, 2005 10:48 am
by wojtas7
Did anybody found a solution for this:

Code: Select all
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.


How to get to the fields inside field symbol which is an internal table?!?!

PostPosted: Thu Feb 17, 2005 11:37 am
by BGEDYNA
data: gt_fieldcat type lvc_t_fcat .
data: gp_table type ref to data .
field-symbols: <gt_table> type table .
field-symbols: <gs_table> .

*Prepare internal table structure
perform prepare_table_structure.

*Create the table
perform create_table.

******************************************
form prepare_Table_structure.
perform append_field using 'MATKL'
'C'
9
' '
1
'Material Group'.
perform append_field using 'BRANDFAM'
'C'
10
' '
2
'Brand'.
perform append_field using 'MATNR'
'C'
18
' '
3
'Material'.
perform append_field using 'MAKTX'
'C'
40
' '
4
'Desc'.
Endform.
*************************************

*************************************
form append_field using name
ftype type c
length type i
dec type i
pos type i
desc .
data: ls_fieldcat type lvc_s_fcat.

ls_fieldcat-fieldname = name.
ls_fieldcat-inttype = ftype.
ls_fieldcat-outputlen = length.
ls_fieldcat-decimals = dec.
append ls_fieldcat to gt_fieldcat.
endform. " append_field


*****************************************

*****************************************
Form create_table
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = gt_fieldcat
importing
ep_table = gp_table.
assign gp_table->* to <gt_table>.
endform.

PostPosted: Fri Feb 18, 2005 4:53 am
by wojtas7
Thanks anyway but in your code how to write value for example to
field <gt_table>-MATKL ??? I need this because incoming IDoc has an unknown structure.

thanks..

PostPosted: Mon Feb 21, 2005 8:11 am
by BGEDYNA
BCALV_TABLE_CREATE.. This progrm is just what yoy neeed to loook at and i m sure u will understand..

Cheers.
BGEDYNA