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

Create tables dynamically

Former Member
0 Likes
1,495

Hi,

I need to create a number of tables (with the same number of fields for each table) depending on my data.

E.g. If I input 6 at the selection screen,

I need to create GT_1, GT_2.... GT_6 with LINE1 TYPE C and LINE2 TYPE C as the fields.

How to do that? Thanks.

Regards,

Janet

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,189

Hi Janet

Here is a sample code to create an internal table dynamically.

TYPE-POOLS:
  abap.
 
DATA:
  gr_structdescr    TYPE REF TO cl_abap_structdescr,
  gr_tabledescr     TYPE REF TO cl_abap_tabledescr,
  gt_components     TYPE abap_component_tab,
  gw_component      TYPE LINE OF abap_component_tab,
  gt_keys           TYPE abap_keydescr_tab,
  gw_key            TYPE LINE OF abap_keydescr_tab,
  gr_wa             TYPE REF TO data,
  gr_itab           TYPE REF TO data.
 
 
FIELD-SYMBOLS:
  <gw_wa>           TYPE ANY,
  <gt_itab>         TYPE ANY TABLE.
 
 
START-OF-SELECTION.
 
* determine components of structure -> GT_COMPONENTS
  MOVE 'COMP1' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_string( ).
  INSERT gw_component INTO TABLE gt_components.
 
  MOVE 'COMP2' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_i( ).
  INSERT gw_component INTO TABLE gt_components.
 
 
* get structure descriptor -> GR_STRUCTDESCR
  gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).
 
 
* create work area of structure GR_STRUCTDESCR -> GR_WA
  CREATE DATA gr_wa TYPE HANDLE gr_structdescr.
 
  ASSIGN gr_wa->* TO <gw_wa>.
 
 
* determine key components -> GT_KEYS
  MOVE 'COMP1' TO gw_key-name.
  INSERT gw_key INTO TABLE gt_keys.
 
 
* create descriptor for internal table -> GR_TABLEDESCR
  gr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type  = gr_structdescr
                                               p_table_kind = cl_abap_tabledescr=>tablekind_hashed
                                               p_unique     = abap_true
                                               p_key        = gt_keys
                                               p_key_kind   = cl_abap_tabledescr=>keydefkind_user ).
 
 
* create internal table -> GR_ITAB
  CREATE DATA gr_itab TYPE HANDLE gr_tabledescr.
 
  ASSIGN gr_itab->* TO <gt_itab>.
 
  sy-subrc = sy-subrc.

Regards

Anup.

11 REPLIES 11
Read only

Former Member
0 Likes
1,189

hi,

To create the tables dynamically we can use the FIELD SYMBOLS.

Where we can assgin the table type ANY type etc.

Pls read more help on FieldSymbols.

There you can find the dynamic creation of tables.

Regards

Chandralekha.

Edited by: Craig Cmehil on Jul 4, 2008 10:31 AM

Read only

pole_li
Active Participant
0 Likes
1,189

Hi,

Check this link: [http://www.sap-img.com/ab030.htm|http://www.sap-img.com/ab030.htm]

Regards,

Pole

Edited by: Pole li on Jul 4, 2008 9:11 AM

Read only

Former Member
0 Likes
1,190

Hi Janet

Here is a sample code to create an internal table dynamically.

TYPE-POOLS:
  abap.
 
DATA:
  gr_structdescr    TYPE REF TO cl_abap_structdescr,
  gr_tabledescr     TYPE REF TO cl_abap_tabledescr,
  gt_components     TYPE abap_component_tab,
  gw_component      TYPE LINE OF abap_component_tab,
  gt_keys           TYPE abap_keydescr_tab,
  gw_key            TYPE LINE OF abap_keydescr_tab,
  gr_wa             TYPE REF TO data,
  gr_itab           TYPE REF TO data.
 
 
FIELD-SYMBOLS:
  <gw_wa>           TYPE ANY,
  <gt_itab>         TYPE ANY TABLE.
 
 
START-OF-SELECTION.
 
* determine components of structure -> GT_COMPONENTS
  MOVE 'COMP1' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_string( ).
  INSERT gw_component INTO TABLE gt_components.
 
  MOVE 'COMP2' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_i( ).
  INSERT gw_component INTO TABLE gt_components.
 
 
* get structure descriptor -> GR_STRUCTDESCR
  gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).
 
 
* create work area of structure GR_STRUCTDESCR -> GR_WA
  CREATE DATA gr_wa TYPE HANDLE gr_structdescr.
 
  ASSIGN gr_wa->* TO <gw_wa>.
 
 
* determine key components -> GT_KEYS
  MOVE 'COMP1' TO gw_key-name.
  INSERT gw_key INTO TABLE gt_keys.
 
 
* create descriptor for internal table -> GR_TABLEDESCR
  gr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type  = gr_structdescr
                                               p_table_kind = cl_abap_tabledescr=>tablekind_hashed
                                               p_unique     = abap_true
                                               p_key        = gt_keys
                                               p_key_kind   = cl_abap_tabledescr=>keydefkind_user ).
 
 
* create internal table -> GR_ITAB
  CREATE DATA gr_itab TYPE HANDLE gr_tabledescr.
 
  ASSIGN gr_itab->* TO <gt_itab>.
 
  sy-subrc = sy-subrc.

Regards

Anup.

Read only

Former Member
0 Likes
1,189

Hi,

can you please elaborate your question.

do you want to create dynamic internal tables or database tables.

For these two the approach is different.

In my earlier post that was the way to create internal tables.

Regards

Chandralekha.

Edited by: Craig Cmehil on Jul 4, 2008 10:31 AM

Read only

Former Member
0 Likes
1,189
Read only

matt
Active Contributor
0 Likes
1,189

Janet - please use the search functionality. This question has been asked time and again. There's plenty of information available and you'll find it quicker than posting a question. It is also part of the rules here to search first, ask second.

Read only

Former Member
0 Likes
1,189

Hi all,

Thanks for the replies. Most of the replies I managed to find from the old forums. However I am not too sure on how to implement this to my problem. Maybe it is because I am not too sure on how to use field symbols and field symbols need to be define first. But I will not know how many field symbol to define until runtime.

Anyway I will check again and assign points again.

Regards,

Janet

Read only

0 Likes
1,189

Hi,

Check the following code it is very similar to your requirment.

type-pools: slis.

field-symbols: <dyn_table> type standard table,

<dyn_wa>.

data: alv_fldcat type slis_t_fieldcat_alv,

it_fldcat type lvc_t_fcat.

selection-screen begin of block b1 with frame title text-001.

parameters: p_flds(5) type c.

selection-screen end of block b1.

start-of-selection.

build the dynamic internal table

perform build_dyn_itab.

write 5 records to the alv grid

do 5 times.

perform build_report.

enddo.

call the alv grid.

perform call_alv.

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

Build_dyn_itab

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

form build_dyn_itab.

data: new_table type ref to data,

new_line type ref to data,

wa_it_fldcat type lvc_s_fcat.

Create fields .

clear wa_it_fldcat.

wa_it_fldcat-fieldname = name1.

wa_it_fldcat-datatype = 'mara-matnr'.

wa_it_fldcat-intlen = 5.

append wa_it_fldcat to it_fldcat .

*

*clear wa_it_fldcat.

wa_it_fldcat-fieldname = sy-index.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 5.

append wa_it_fldcat to it_fldcat .

*

do p_flds times.

clear wa_it_fldcat.

wa_it_fldcat-fieldname = sy-index.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 6.

append wa_it_fldcat to it_fldcat .

enddo.

Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fldcat

importing

ep_table = new_table.

assign new_table->* to <dyn_table>.

Create dynamic work area and assign to FS

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

endform.

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

Form build_report

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

form build_report.

data: fieldname(20) type c.

data: fieldvalue(5) type c.

data: index(3) type c.

field-symbols: <fs1>.

do p_flds times.

index = sy-index.

Set up fieldvalue

concatenate 'FLD' index into

fieldvalue.

condense fieldvalue no-gaps.

assign component index of structure <dyn_wa> to <fs1>.

<fs1> = fieldvalue.

enddo.

Append to the dynamic internal table

append <dyn_wa> to <dyn_table>.

endform.

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

CALL_ALV

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

form call_alv.

data: wa_cat like line of alv_fldcat.

*clear wa_cat.

wa_cat-fieldname = matnr.

wa_cat-seltext_s = sy-index.

wa_cat-outputlen = 'ma'.

append wa_cat to alv_fldcat.

*

do p_flds times.

clear wa_cat.

wa_cat-fieldname = sy-index.

wa_cat-seltext_s = sy-index.

wa_cat-outputlen = '6'.

append wa_cat to alv_fldcat.

enddo.

Call ABAP List Viewer (ALV)

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

it_fieldcat = alv_fldcat

tables

t_outtab = <dyn_table>.

endform.

Regards,

Ram.

Edited by: Craig Cmehil on Jul 7, 2008 9:08 AM

Read only

0 Likes
1,189

Lee,

Better follow this code I have tested this it is better written than the previous one.

type-pools: slis.

field-symbols: <dyn_table> type standard table,

<dyn_wa>.

data: alv_fldcat type slis_t_fieldcat_alv,

it_fldcat type lvc_t_fcat.

selection-screen begin of block b1 with frame title text-001.

parameters: p_flds(5) type c.

selection-screen end of block b1.

start-of-selection.

*build the dynamic internal table

perform build_dyn_itab.

*write 5 records to the alv grid

do 5 times.

perform build_report.

enddo.

*call the alv grid.

perform call_alv.

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

*Build_dyn_itab

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

form build_dyn_itab.

data: new_table type ref to data,

new_line type ref to data,

wa_it_fldcat type lvc_s_fcat.

*Create fields .

clear wa_it_fldcat.

wa_it_fldcat-fieldname = 'name1'.

wa_it_fldcat-datatype = 'mara-matnr'.

wa_it_fldcat-intlen = 5.

append wa_it_fldcat to it_fldcat .

*

*clear wa_it_fldcat.

wa_it_fldcat-fieldname = sy-index.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 5.

append wa_it_fldcat to it_fldcat .

*

do p_flds times.

clear wa_it_fldcat.

wa_it_fldcat-fieldname = sy-index.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 6.

append wa_it_fldcat to it_fldcat .

enddo.

*Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fldcat

importing

ep_table = new_table.

assign new_table->* to <dyn_table>.

*Create dynamic work area and assign to FS

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

endform.

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

*Form build_report

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

form build_report.

data: fieldname(20) type c.

data: fieldvalue(5) type c.

data: index(3) type c.

field-symbols: <fs1>.

do p_flds times.

index = sy-index.

*Set up fieldvalue

concatenate 'FLD' index into

fieldvalue.

condense fieldvalue no-gaps.

assign component index of structure <dyn_wa> to <fs1>.

<fs1> = fieldvalue.

enddo.

*Append to the dynamic internal table

append <dyn_wa> to <dyn_table>.

endform.

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

*CALL_ALV

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

form call_alv.

data: wa_cat like line of alv_fldcat.

*clear wa_cat.

wa_cat-fieldname = 'matnr'.

wa_cat-seltext_s = sy-index.

wa_cat-outputlen = '10'.

append wa_cat to alv_fldcat.

*

do p_flds times.

clear wa_cat.

wa_cat-fieldname = sy-index.

wa_cat-seltext_s = sy-index.

wa_cat-outputlen = '6'.

append wa_cat to alv_fldcat.

enddo.

*Call ABAP List Viewer (ALV)

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

it_fieldcat = alv_fldcat

tables

t_outtab = <dyn_table>.

endform.

Regards,

Ram.

Read only

0 Likes
1,189

Hi,

Is the program creating the fields dynamically? Actually I know the number of fields. It is a fixed number of fields. I need the multiple internal table to be created instead.

E.G.

I have following internal table definition:

DATA: BEGIN OF GT_TAB1 OCCURS 0,

LINE1 TYPE C,

LINE2 TYPE C,

END OF GT_TAB1.

I need multiple internal tables (GT_TAB2, GT_TAB3....) with a structure of GT_TAB1 to be created based on the selection screen.

Regards,

Janet

Read only

0 Likes
1,189

Hi Lee,

The following code shoul help the purpose.

This code explains about the dynamic creation of internal table according to the input database table and displaying it in an ALV Grid. Works with all sort of database tables (even huge number of records).This concept can be expanded to work with tables that have very large number of fields.

By: Siva Subramanian Kalaiselvan

Company: Wipro Technologies

Date: 21 Jan. 2005

Code Sample

REPORT ZC3_DYNITABDISPALV MESSAGE-ID zmsgdynamic.

----


  • Description : Dynamic internal table creation and

  • displaying in ALV Grid.

  • Technical Contact : SivaSubramanian Kalaiselvan

  • Technical Spec. Number: ZC3_DYNITABDISPALV.

  • Created on : 21/01/05.

  • Transport request : PDCK902348.

  • Development Class : zc3_sivdev.

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

TABLES : tadir. " table in which the obj_name field stores all the

" database table names

TYPE-POOLS : slis.

TYPES ztab LIKE dcobjdef-name .

DATA : dyntab LIKE dntab OCCURS 0 WITH HEADER LINE,

dref TYPE REF TO data,

i_fcat TYPE lvc_t_fcat ,

wa_fcat TYPE lvc_s_fcat ,

w_pgm LIKE sy-repid,

count TYPE i VALUE 0,

w_obj_name LIKE tadir-obj_name.

DATA : wa_fieldcat TYPE slis_fieldcat_alv, "ALV FIELD CATALOG TABLE

i_fieldcat TYPE slis_t_fieldcat_alv, "ALV FIELD CATALOG STRUCTURE

i_event TYPE slis_t_event, "ALV EVENT HANDLING TABLE

wa_event TYPE slis_alv_event, "ALV EVENT HANDLING STRUCT

i_listheader TYPE slis_t_listheader, "ALV LIST HEADER TABLE

wa_listheader TYPE slis_listheader, "ALV LIST HEADER STRUCT

i_layout TYPE slis_layout_alv.

FIELD-SYMBOLS :<newtab> TYPE table.

INITIALIZATION.

w_pgm = sy-repid.

****************SELECTION-SCREEN 2000

SELECTION-SCREEN BEGIN OF SCREEN 2000 AS WINDOW.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE tit1.

PARAMETERS: tab_name TYPE ztab . " Enter database table name

SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN END OF SCREEN 2000.

***************END OF SELECTION-SCREEN 2000

tit1 = 'Enter Table Name?'.

START-OF-SELECTION.

CALL SELECTION-SCREEN 2000 STARTING AT 10 10.

PERFORM occurcheck.

*********CHECKING WHETHER THE DATABASE TABLE EXISTS

FORM occurcheck.

SELECT obj_name FROM tadir INTO w_obj_name WHERE obj_name = tab_name.

ENDSELECT.

IF sy-subrc <> 0.

MESSAGE i003."Table not found in the database .

CALL SELECTION-SCREEN 2000 STARTING AT 10 10.

PERFORM occurcheck.

ELSE.

PERFORM startprocess.

EXIT.

ENDIF.

ENDFORM.

**********GETTING THE FIELD NAMES OF THE INPUT DATABASE TABLE

FORM startprocess.

CALL FUNCTION 'NAMETAB_GET'

EXPORTING

langu = sy-langu

tabname = tab_name

TABLES

nametab = dyntab. "dntab now contains the field names

**********FILLING THE CATALOG OF NEW DYNAMIC INTERNAL TABLE

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.

***********CREATING A POINTER (FIELD SYMBOL) TO THE INTERNAL TABLE

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = i_fcat

IMPORTING

ep_table = dref.

ASSIGN dref->* TO <newtab>.

SELECT * FROM (dyntab-tabname) INTO TABLE <newtab>.

**********CHECKING WHETHER DATA HAS BEEN UPLOADED.

DESCRIBE TABLE <newtab> LINES sy-tfill.

DESCRIBE TABLE <newtab> LINES count.

IF sy-tfill = 0 .

MESSAGE i001. "Internal table is not filled

ELSE.

MESSAGE i002 WITH count. "Internal table is filled

ENDIF.

****************EVENTS USED IN ALV

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

EXPORTING

i_list_type = 0 " 0-simple list, 1-hierarchial list.

IMPORTING

et_events = i_event.

SORT i_event.

READ TABLE i_event

WITH KEY name = slis_ev_top_of_page "TOP_OF_PAGE event

INTO wa_event.

IF sy-subrc = 0.

MOVE 'IAM_TOP_OF_PAGE' TO wa_event-form. "IAM_TOP_OF_PAGE will

"call subroutine automatically

MODIFY i_event FROM wa_event INDEX sy-tabix.

ENDIF.

****************DISPLAYING ALV.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = w_pgm

i_structure_name = dyntab-tabname

it_events = i_event

TABLES

t_outtab = <newtab>.

ENDFORM.

**********FORM TOP_OF-PAGE.

******FILLING OF LIST HEADER TABLE

FORM iam_top_of_page.

CLEAR wa_listheader.

wa_listheader-typ = 'H'. "HEADING

wa_listheader-info = 'ALV CREATED DYNAMICALLY'.

APPEND wa_listheader TO i_listheader.

CLEAR wa_listheader.

wa_listheader-typ = 'S'. "SUB-HEADING

wa_listheader-key = 'PGM NAME:'.

wa_listheader-info = w_pgm.

APPEND wa_listheader TO i_listheader.

CLEAR wa_listheader.

wa_listheader-typ = 'S'.

wa_listheader-key = 'USER:'.

wa_listheader-info = sy-uname.

APPEND wa_listheader TO i_listheader.

CLEAR wa_listheader.

wa_listheader-typ = 'S'.

wa_listheader-key = 'DATE:'.

wa_listheader-info = sy-datum.

APPEND wa_listheader TO i_listheader.

CLEAR wa_listheader.

wa_listheader-typ = 'S'.

wa_listheader-key = 'TABLE NAME:'.

wa_listheader-info = dyntab-tabname.

APPEND wa_listheader TO i_listheader.

********FUNCTION TO WRITE THE TITLE AND SUB-TITLE OF THE ALV.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'

EXPORTING

it_list_commentary = i_listheader.

ENDFORM.

Reward if found usefull......!

Regards,

Ram.