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 Dynamic Internal Table

Former Member
0 Likes
864

Can anyone tell me the way to create dynamic internal table?

I want to give the table name at selection screen and then fetch data from tht table into my internal table.

thanks in advance

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
776

use thsi piece of code and reward if helpfull -

&----


*& Report ZGILL_FS *

*& *

&----


*& *

*& *

&----


REPORT ZGILL_FS line-size 250

line-count 65 .

************Creating Dynamic internal table************

parameter p_table type tabname.

field-symbols <tab> type table.

field-symbols <tab1> type any.

types: begin of itab,

t_name type tabname,

t_ref type ref to data,

end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.

perform print_table using p_table.

&----


*& Form fetch_data

&----


  • text

----


  • -->P_P_TABLE text

----


FORM fetch_data USING P_TABLE1 type tabname.

data itab2 type itab.

itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM. " fetch_data

&----


*& Form print_table

&----


  • text

----


  • -->P_P_TABLE text

----


FORM print_table USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.

DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.

*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.

READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.

*WRITE:/ p_table1.

NEW-LINE.

IF sy-subrc <> 0.

EXIT.

ENDIF.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

WRITE: <field>,' '.

ENDDO.

ENDDO.

ENDFORM. " print_table

5 REPLIES 5
Read only

Former Member
0 Likes
777

use thsi piece of code and reward if helpfull -

&----


*& Report ZGILL_FS *

*& *

&----


*& *

*& *

&----


REPORT ZGILL_FS line-size 250

line-count 65 .

************Creating Dynamic internal table************

parameter p_table type tabname.

field-symbols <tab> type table.

field-symbols <tab1> type any.

types: begin of itab,

t_name type tabname,

t_ref type ref to data,

end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.

perform print_table using p_table.

&----


*& Form fetch_data

&----


  • text

----


  • -->P_P_TABLE text

----


FORM fetch_data USING P_TABLE1 type tabname.

data itab2 type itab.

itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM. " fetch_data

&----


*& Form print_table

&----


  • text

----


  • -->P_P_TABLE text

----


FORM print_table USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.

DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.

*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.

READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.

*WRITE:/ p_table1.

NEW-LINE.

IF sy-subrc <> 0.

EXIT.

ENDIF.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

WRITE: <field>,' '.

ENDDO.

ENDDO.

ENDFORM. " print_table

Read only

Former Member
0 Likes
776

Try this,


REPORT  zkb_dynamic_itab.

TYPE-POOLS : abap.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
               <dyn_wa>,
               <dyn_field>.

DATA: dy_table TYPE REF TO data,
      dy_line  TYPE REF TO data,
      xfc TYPE lvc_s_fcat,
      ifc TYPE lvc_t_fcat.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_table(30) TYPE c DEFAULT 'SCARR'.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

  PERFORM get_structure.
  PERFORM create_dynamic_itab.
  PERFORM get_data.
  PERFORM read_data_from_dyntable USING 'JL'.

*&---------------------------------------------------------------------*
*&      Form  get_structure
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_structure.
  DATA : idetails TYPE abap_compdescr_tab,
         xdetails TYPE abap_compdescr.

  DATA : ref_table_des TYPE REF TO cl_abap_structdescr.

* Get the structure of the table.
  ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( p_table ).
  idetails[] = ref_table_des->components[].

  LOOP AT idetails INTO xdetails.
    CLEAR xfc.
    xfc-fieldname = xdetails-name .
    xfc-datatype = xdetails-type_kind.
    xfc-inttype = xdetails-type_kind.
    xfc-intlen = xdetails-length.
    xfc-decimals = xdetails-decimals.
    APPEND xfc TO ifc.
  ENDLOOP.

ENDFORM.                    "get_structure
*&---------------------------------------------------------------------*
*&      Form  create_dynamic_itab
*&---------------------------------------------------------------------*
*       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 = ifc
    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  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_data.
* Select Data from table.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table>
             FROM (p_table).
ENDFORM.                    "get_data
*&---------------------------------------------------------------------*
*&      Form  read_data_from_dyntable
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_CARRID  text
*----------------------------------------------------------------------*
FORM read_data_from_dyntable  USING    p_carrid TYPE s_carr_id.
  FIELD-SYMBOLS: <l_dyn_wa> TYPE ANY,
                 <l_dyn_field> TYPE ANY.

  DATA: l_dy_line  TYPE REF TO data.
  DATA: li_results TYPE match_result_tab,
        lw_results TYPE match_result.

  CREATE DATA l_dy_line LIKE LINE OF <dyn_table>.
  ASSIGN l_dy_line->* TO <l_dyn_wa>.
  ASSIGN COMPONENT 2 OF STRUCTURE <l_dyn_wa> TO <l_dyn_field>.
  <l_dyn_field> = p_carrid.

* Reading the data
  FIND ALL OCCURRENCES OF <l_dyn_field>
       IN TABLE <dyn_table>
       RESULTS li_results.

  READ TABLE li_results INTO lw_results INDEX 1.
  READ TABLE <dyn_table> INTO <dyn_wa> INDEX lw_results-line.

  WRITE:/ <dyn_wa>.

* Modifying the data
  ASSIGN COMPONENT 5 OF STRUCTURE <dyn_wa> TO <l_dyn_field>.
  <l_dyn_field> = 'http://www.jal.com'.
  WRITE:/ <dyn_wa>.

ENDFORM.                    " read_data_from_dyntable

Regards

Kathirvel

Read only

Former Member
0 Likes
776

Check this Documentation excerpt:

"CREATE DATA - TABLE OF

Syntax

CREATE DATA dref { {TYPE tabkind OF [REF TO] {type|(name)}}

| {LIKE tabkind OF dobj} }

[WITH key]

[INITIAL SIZE n].

Effect

The addition tabkind OF causes the CREATE DATA statement to generate an internal table. The additions have the same meanings as in the declaration of internal tables with the DATA statement. In particular, the explicit definition of the table key is only optional when you generate a standard table.

Whereas all the specifications for DATA have to be static, you can use the following dynamic specifications for CREATE DATA:

The row type after TYPE or the static type of a row flagged as a reference variable after TYPE REF TO can have a character-like data object called name. In this case, the same rules apply as previously described.

You can also specify an internal table keytab in addition to the static component comp1 comp2 ... in the specification of table key key after WITH:

... WITH [UNIQUE|NON-UNIQUE] KEY (keytab) ...

Table keytab must have a character-type data type and must contain the name of a valid component in each row or the identifier table_line for the table key in a single row.

You can specify a numeric data object for n after INITIAL SIZE.

Example

Generation of an internal table for any database table and import of the first rows of that database table into the internal table. Since the data reference dref has a dynamic type, you have to use the field symbol to access the internal table. Field symbols <wa> and <comp> are required to dynamically access the individual rows and components.

PARAMETERS: dbtab(10) TYPE c,

rows TYPE i DEFAULT 100.

DATA dref TYPE REF TO data.

FIELD-SYMBOLS: TYPE ANY TABLE,

<wa> TYPE ANY,

TYPE ANY.

TRY.

CREATE DATA dref TYPE STANDARD TABLE OF (dbtab)

WITH NON-UNIQUE DEFAULT KEY.

ASSIGN dref->* TO .

SELECT *

FROM (dbtab) UP TO rows ROWS

INTO TABLE .

LOOP AT ASSIGNING <wa>.

DO.

ASSIGN COMPONENT sy-index

OF STRUCTURE <wa> TO .

IF sy-subrc = 0.

WRITE / .

ELSE.

EXIT.

ENDIF.

ENDDO.

ULINE.

ENDLOOP.

CATCH cx_sy_create_data_error.

WRITE 'Wrong Database!'.

ENDTRY.

"

regards,

Ravi

Read only

0 Likes
776

refer this thread u get lots of ex-.

Read only

Former Member
0 Likes
776

Go through the links below.

/people/ravikumar.allampallam/blog/2005/05/31/expand-the-list-of-columns-in-a-report-dynamically

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap