Application Development 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: 

Dynamic Internal Table

Former Member
0 Kudos

Hi Friends,

My requirement is..........

  1. i have one internal table call ITAB1 with 10 components(fields)

  2. i want to add another columns to ITAB1 dynamically (no of columns defends with select option)

  3. now i want to fill data to ITAB1 (explain how to catch dynamically created fields)

  4. now i want to display as a ALV.

Im new to this topic (Dynamic internal table) so please explain step by step...

Thanks in Advance

Nelson

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

Dynamic internal table is internal table that we create on the fly with flexible column numbers.

For sample code, please look at this code tutorial. Hopefully it can help you

Check this link:

http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm

<b>Reward points for useful Answers</b>

Regards

Anji

9 REPLIES 9

Former Member
0 Kudos

Hi

Dynamic internal table is internal table that we create on the fly with flexible column numbers.

For sample code, please look at this code tutorial. Hopefully it can help you

Check this link:

http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm

<b>Reward points for useful Answers</b>

Regards

Anji

former_member196299
Active Contributor
0 Kudos

hi Nelson ,

check this threda :

Regards,

Ranjita

Former Member
0 Kudos

Hi Nelson

I can tell you the exact steps upto point number 3 i.e to get data into an internal table dynamically using select options:

REPORT z_fieldsymbol.

  • Data declaration and selection screen

INCLUDE z_fieldsymbol_top.

  • Forms definitions

INCLUDE z_fieldsymbol_forms.

*----


  • Start-Of-Selection

*----


START-OF-SELECTION .

  • Creating structure of dynamic table

PERFORM f_create_table USING p_table.

  • Creating internal table for dynamic table

PERFORM create_dynamic_itab.

  • Logic to perform conversion exit for differnet fields

PERFORM conv_logic.

  • To fetch data from database table into dynamic internal tables

PERFORM get_data .

===========================================================

*&----


*

*& Include Z_FIELDSYMBOL_TOP

&----


  • DECLARING STRUCTURE *

TYPE-POOLS : abap.

  • DECLARING THE TABLE FOR DYNAMIC FIELDS *

TABLES: dd01l,dd03l. "#EC NEEDED)

  • DECLARING FIELD SYMBOL FOR PERFORMING DYNAMIC SELECTION *

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE ,

<dyn_wa> TYPE ANY,

<dyn_field> TYPE ANY.

  • DECLARING CONSTANT FOR WHERE CLAUSE *

CONSTANTS: c_var(15) TYPE c VALUE ' IN S_RANGE'.

  • DECLARING DATA FIELDS *

DATA: dy_table TYPE REF TO data, "variable used to build table str

dy_line TYPE REF TO data, "variable for dynamic table creation

it_fcat TYPE lvc_t_fcat, "internal table to create dynamic table

v_where TYPE string, " Variable for storing where clause.

v_dynamic(18) TYPE c, "variable to store select option datatype

o_field TYPE REF TO cx_root," object to catch exception

text TYPE string. "string variable to store exception text.

*DATA: wa_srange like v_dynamic.

  • DECLARATION OF SELECTION SCREEN ELEMENT *

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.

PARAMETERS:

p_table type dd02l-tabname, " Parameter to capture table name.

p_field(10) TYPE c. " Parameter to capture field name.

SELECT-OPTIONS: s_range FOR v_dynamic. " Select-option for range.

PARAMETERS:

p_fl_chg(20) TYPE c, " Parameter to capture field to be changed.

p_value(20) TYPE c. " Parameter to store value to be updated.

SELECTION-SCREEN END OF BLOCK b1.

  • Assigning data objects to a field symbols

CONCATENATE p_field c_var INTO v_where.

===========================================================

&----


*& Include Z_FIELDSYMBOL_FORMS

&----


&----


*& Form f_create_table

&----


  • text

----


  • -->P_P_TABLE text

----


FORM f_create_table USING in_tabname. "#EC *)

  • FIELD-SYMBOLS: <fcat> TYPE lvc_s_fcat.

DATA: wa_fcat LIKE LINE OF it_fcat.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = in_tabname

CHANGING

ct_fieldcat = it_fcat

EXCEPTIONS

OTHERS = 1.

IF sy-subrc = 0.

MOVE: in_tabname TO wa_fcat-tabname.

MODIFY it_fcat FROM wa_fcat TRANSPORTING tabname WHERE fieldname IS

NOT INITIAL.

ELSE.

WRITE: text-t01.

LEAVE PROGRAM.

ENDIF.

ENDFORM. " f_create_table

&----


*& Form create_dynamic_itab

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM create_dynamic_itab .

  • Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fcat

IMPORTING

ep_table = dy_table.

ASSIGN dy_table->* TO <dyn_table>.

  • Create dynamic work area and assign to FS

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

ENDFORM. " create_dynamic_itab

&----


*& Form Conv_logic

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM conv_logic .

  • Declaration of local data.

DATA:

v_convexit(30) TYPE c, " Variable to fetch & store conversion exit.

v_domname(20) TYPE c, " Variable to store domain of field.

wa_srange LIKE s_range.

  • Select statement to get the domain name of the field for identifying

  • the conversion exit

SELECT domname

INTO

v_domname FROM

dd03l

WHERE tabname = p_table AND fieldname = p_field .

ENDSELECT.

  • Select statement to get the conversion exit name for the field

SELECT convexit

INTO v_convexit

FROM dd01l

WHERE domname = v_domname.

ENDSELECT.

  • If conversion exit found for that domain Concatenate it into a

  • variable to make it dynamic.

IF v_convexit NE ' '.

CONCATENATE 'CONVERSION_EXIT_'

v_convexit

'_INPUT'

INTO

v_convexit.

  • A perform called to call a conversion exit function module

LOOP AT s_range INTO wa_srange.

PERFORM f_conversion_exit

USING p_table

p_field

v_convexit :

CHANGING wa_srange-low,

wa_srange-high .

MODIFY s_range FROM wa_srange.

ENDLOOP.

ENDIF.

ENDFORM. " Conv_logic

&----


*& Form f_conversion_exit

&----


  • text

----


  • -->P_P_TABLE text

  • -->P_P_FIELD text

  • -->P_VARCONV_EXIT text

  • <--P_S_RANGE_LOW text

----


FORM f_conversion_exit USING p_tabname "#EC *)

p_fname "#EC *)

p_convexit "#EC *)

CHANGING p_value.

  • Declare a field symbol which has a field type structure

FIELD-SYMBOLS: <fs_fld> TYPE ANY.

ASSIGN COMPONENT p_field OF STRUCTURE <dyn_wa> TO <fs_fld>."#EC * )

CALL FUNCTION p_convexit

EXPORTING

input = p_value

IMPORTING

output = <fs_fld>.

MOVE <fs_fld> TO p_value.

ENDFORM. " f_conversion_exit

&----


*& Form get_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_data .

TRY.

SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table>

FROM (p_table) WHERE (v_where) .

IF sy-dbcnt = 0.

WRITE : text-t02.

ENDIF.

  • Exception Catching.

CATCH cx_root INTO o_field.

text = o_field->get_text( ).

  • Calling Function to give information message regarding Exception

CALL FUNCTION 'POPUP_TO_INFORM'

EXPORTING

titel = text-t03

txt1 = text

txt2 = text-t04.

  • TXT3 = ' '

  • TXT4 = ' '

LEAVE TO LIST-PROCESSING.

ENDTRY.

ENDFORM. " get_data

===========================================================

Upto this point, you can select your table name dynamically, the filed name dynamically and fetch a range of data into a dynamic internal table and subsequently into an internal table.

<b>

Reward if useful</b>

Regards

Ravish Garg

Former Member
0 Kudos

u know the no of fileds in the existing internal table and u have to fill the fieldcatalog and call method and it will create table with the newly propsoed fields and use the same to output

data: it_fcat TYPE lvc_t_fcat, "Field catalog for table

v_dref1 TYPE REF TO data, " Data ref

v_new_line1 TYPE REF TO data. " Data ref

*--- Get table details

PERFORM f_gettab_details.

*--- create the dynamic internal table to display

PERFORM f_create_dyntab.

&----


*& Form f_gettab_details

&----


*Form to get final internal table fields

----


FORM f_gettab_details.

DATA: lv_count TYPE char2,

lv_name LIKE lvc_s_fcat-fieldname.

*--- Material Number

CLEAR x_fcat.

x_fcat-fieldname = 'MATNR'.

x_fcat-ref_field = 'MATNR'.

x_fcat-ref_table = 'MARA'.

APPEND x_fcat TO it_fcat .

*--- Material Text

CLEAR x_fcat.

x_fcat-fieldname = 'MAKTX'.

x_fcat-ref_field = 'MAKTX'.

x_fcat-ref_table = 'MAKT'.

APPEND x_fcat TO it_fcat .

*--- Batch No

CLEAR x_fcat.

x_fcat-fieldname = 'CHARG'.

x_fcat-ref_field = 'CHARG'.

x_fcat-ref_table = 'MCHB'.

APPEND x_fcat TO it_fcat .

*--- Storage location

CLEAR x_fcat.

x_fcat-fieldname = 'LGORT'.

x_fcat-ref_field = 'LGORT'.

x_fcat-ref_table = 'MCHB'.

APPEND x_fcat TO it_fcat .

*--- Stock

CLEAR x_fcat.

x_fcat-fieldname = 'STOCK'.

x_fcat-datatype = 'CHAR'.

x_fcat-intlen = '17'.

APPEND x_fcat TO it_fcat .

endform.

&----


*& Form f_create_dyntab

&----


  • text

----


FORM f_create_dyntab.

CLEAR: v_dref1,

v_dref2.

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

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fcat

IMPORTING

ep_table = v_dref1.

ASSIGN: v_dref1->* TO <fs_tab>.

CREATE DATA v_new_line1 LIKE LINE OF <fs_tab>.

ASSIGN: v_new_line1->* TO <fs_wa>.

endform.

Former Member
0 Kudos

Former Member
0 Kudos

hi,

chk the below code and link.

very hlpful

REPORT zdynarjtry.

PARAMETERS: comp(80).

DATA: dref TYPE REF TO data.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE.

CREATE DATA dref TYPE TABLE OF (comp).

ASSIGN dref->* TO <dyn_table>.

SELECT * FROM (comp)

INTO TABLE <dyn_table>.

BREAK-POINT.

See the below links for more programs:-

http://searchsap.techtarget.com/tip/1,289483,sid21_gci912390,00.html

http://www.sap-img.com/ab030.htm

Check this link:

http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm

<b>Reward points</b>

Regards

Pawan_Kesari
Active Contributor
0 Kudos


REPORT zpwtest .

*** Tables
DATA: lt_data TYPE REF TO data.
DATA: lt_fieldcatalog TYPE lvc_t_fcat.

data : p_field type string ,
       p_table type string .

*** Structure
DATA: ls_fieldcatalog TYPE lvc_s_fcat.

*** Data References
DATA: new_line TYPE REF TO data.

*** Field Symbols
FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
               <fs_1> TYPE ANY TABLE,
               <fs_2>,
               <fs_3>.



ls_fieldcatalog-fieldname = 'MANDT'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'CARRID'. "Fieldname
ls_fieldcatalog-inttype = 'C'. "Internal Type C-> Character
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'CONNID'.
ls_fieldcatalog-inttype = 'N'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'FLDATE'.
ls_fieldcatalog-inttype = 'D'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'PRICE'.
ls_fieldcatalog-inttype = 'P'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'CURRENCY'.
ls_fieldcatalog-inttype = 'C'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.


ASSIGN lt_data TO <fs_data>.


CALL METHOD cl_alv_table_create=>create_dynamic_table
     EXPORTING
       it_fieldcatalog = lt_fieldcatalog
     IMPORTING
       ep_table = <fs_data>
     EXCEPTIONS
       generate_subpool_dir_full = 1
       OTHERS = 2
		.
IF sy-subrc <> 0.
ENDIF.


*** So <FS_1> now points to our dynamic internal table.
ASSIGN <fs_data>->* TO <fs_1>.

*** Next step is to create a work area for our dynamic internal table.

CREATE DATA new_line LIKE LINE OF <fs_1>.

*** A field-symbol to access that work area
ASSIGN new_line->*  TO <fs_2>.

p_field = 'mandt carrid connid fldate price currency' .
p_table = 'sflight' .

*** And to put the data in the internal table
SELECT (p_field)
  FROM (p_table)
  INTO CORRESPONDING FIELDS OF TABLE <fs_1>.

*** Access contents of internal table
LOOP AT <fs_1> ASSIGNING <fs_2>.
  ASSIGN COMPONENT 5 OF STRUCTURE <fs_2> TO <fs_3>.
  WRITE: / <fs_3>.
ENDLOOP.

0 Kudos


REPORT  zpwtest1                                .


*** Tables
DATA: lt_data TYPE REF TO data.
DATA: lt_fieldcatalog TYPE lvc_t_fcat.

*** Structure
DATA: ls_fieldcatalog TYPE lvc_s_fcat.

*** Data References
DATA: new_line TYPE REF TO data.

*** Field Symbols
FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
               <fs_1> TYPE STANDARD TABLE,
               <fs_2>,
               <fs_3>.



ls_fieldcatalog-fieldname = 'MANDT'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'CARRID'. "Fieldname
ls_fieldcatalog-inttype = 'C'. "Internal Type C-> Character
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'CONNID'.
ls_fieldcatalog-inttype = 'N'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'FLDATE'.
ls_fieldcatalog-inttype = 'D'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'PRICE'.
ls_fieldcatalog-inttype = 'P'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'CURRENCY'.
ls_fieldcatalog-inttype = 'C'.
APPEND ls_fieldcatalog TO lt_fieldcatalog.


ASSIGN lt_data TO <fs_data>.


CALL METHOD cl_alv_table_create=>create_dynamic_table
     EXPORTING
       it_fieldcatalog = lt_fieldcatalog
     IMPORTING
       ep_table = <fs_data>
     EXCEPTIONS
       generate_subpool_dir_full = 1
       OTHERS = 2
		.
IF sy-subrc <> 0.
ENDIF.

*** So <FS_1> now points to our dynamic internal table.

ASSIGN <fs_data>->* TO <fs_1>.

*** Next step is to create a work area for our dynamic internal table.

CREATE DATA new_line LIKE LINE OF <fs_1>.

*** A field-symbol to access that work area
ASSIGN new_line->*  TO <fs_2>.

*** And to put the data in the internal table
SELECT mandt carrid connid fldate price currency
  FROM sflight
  INTO CORRESPONDING FIELDS OF TABLE <fs_1>.

*** Access contents of internal table

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    it_fieldcat_lvc = lt_fieldcatalog
  TABLES
    t_outtab        = <fs_1>.

Former Member
0 Kudos

Hi Frends

now i can manage , thanks for all of your postings and i rewarded you.