This website is not affiliated with, sponsored by, or approved by SAP AG.

How to declare an Internal table dynamically?

Development (ABAP Development WorkBench, ABAP/4 programming)

Moderators: Snowy, thx4allthefish, YuriT, Gothmog

How to declare an Internal table dynamically?

Postby nazgul » Sun Apr 27, 2003 10:18 pm

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 :).
Regards,
nazgul
nazgul
 
Posts: 41
Joined: Mon Jan 27, 2003 10:52 pm

Postby Vijayendra » Sun Apr 27, 2003 10:25 pm

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
Dare to Share
Vijayendra
 
Posts: 221
Joined: Mon Dec 23, 2002 1:07 am
Location: Singapore

Postby projecttoday » Mon Apr 28, 2003 12:50 am

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
projecttoday
 
Posts: 166
Joined: Sat Nov 23, 2002 12:42 pm
Location: West Virginia, USA

Postby kumar » Mon Apr 28, 2003 3:14 am

Read the Manual for the Syntax

GENERATE SUBROUTINE POOL CODE NAME PROG

It is possible to create Dynmic Internal Tables.

KRK
kumar
 
Posts: 2012
Joined: Tue Oct 22, 2002 12:51 pm
Location: Basel, Switzerland

code ....

Postby anandksap » Mon Apr 28, 2003 3:54 am

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

Use the SEARCH facility.

Postby Dr Sidewalk » Mon Apr 28, 2003 6:09 am

This question must get asked at least once a week!!!
"In the middle of difficulty lies opportunity" - Albert Einstein
"Money isn't everything in life, unless you don't have it"
"Fail to plan, plan to fail"
"Success is a journey, not a destination."
Dr Sidewalk
 
Posts: 3531
Joined: Mon Oct 21, 2002 11:06 am
Location: Everywhere...

Postby nazgul » Sun May 04, 2003 9:57 pm

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!
Regards,
nazgul
nazgul
 
Posts: 41
Joined: Mon Jan 27, 2003 10:52 pm

Postby nazgul » Mon May 05, 2003 1:55 am

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.
Regards,
nazgul
nazgul
 
Posts: 41
Joined: Mon Jan 27, 2003 10:52 pm

This works to

Postby dalzio » Tue Apr 13, 2004 6:32 am

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.

Kind Regards

Gianpietro dal Zio

dalzio(at)aeb.de
http://www.aeb.biz
dalzio
 
Posts: 9
Joined: Wed Mar 10, 2004 8:26 am
Location: Stuttgart

How to declare an Internal table dynamically?

Postby sk_pathare » Tue Feb 01, 2005 2:22 am

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 : /.
Regards,
Sandy
sk_pathare
 
Posts: 3
Joined: Tue Feb 01, 2005 12:57 am
Location: mumbai

Postby Rich » Tue Feb 01, 2005 3:38 am

Ummm... The answer is a click away in the Abappers Knowledge Corner.
Regards

Rich

Image
Abap KC:http://www.richard-harper.me.uk/Kb
SFMDR:http://www.se37.com
Rich
 
Posts: 7112
Joined: Thu Oct 31, 2002 4:47 pm
Location: Liverpool

Stefan90

Postby wojtas7 » Thu Feb 17, 2005 10:48 am

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?!?!
greets
wojtas7
 
Posts: 35
Joined: Wed Dec 29, 2004 8:51 am

Postby BGEDYNA » Thu Feb 17, 2005 11:37 am

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

Postby wojtas7 » Fri Feb 18, 2005 4:53 am

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..
greets
wojtas7
 
Posts: 35
Joined: Wed Dec 29, 2004 8:51 am

Postby BGEDYNA » Mon Feb 21, 2005 8:11 am

BCALV_TABLE_CREATE.. This progrm is just what yoy neeed to loook at and i m sure u will understand..

Cheers.
BGEDYNA
BGEDYNA
 

Next

Return to ABAP

Who is online

Users browsing this forum: Exabot [Bot] and 11 guests





loading...


This website is not affiliated with, sponsored by, or approved by SAP AG.