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

Dynamic Internal Table

Former Member
0 Likes
947

Hi all,

I need your help in some coding.

I need to input the name of a table and retrieve some fields from that table in an

internal table. That can be done like:

parameters : p_tab like dd03l-tabname.

select * from (p_tab) into table itab.

Now my problem is that HOW SHALL I DECLARE THE INTERNAL TABLE ITAB ??

Like if the table name entered is MARA, then itab should be of type MARA, but what when I dont know the name of the table name beforehand???

Please help.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
901
try this..

 
DATA:
  gdo_data      TYPE REF TO data.
 
FIELD-SYMBOLS:
  <gt_itab>     TYPE table,
  <gs_entry>    TYPE ANY.
 
 
PARAMETERS:
  p_table    TYPE tabname    DEFAULT 'KNA1',
  p_fld      TYPE fieldname  DEFAULT 'KUNNR'.
 
 
START-OF-SELECTION.
 
  CREATE DATA gdo_data TYPE TABLE OF (p_table).
  ASSIGN gdo_data->* TO <gt_itab>.
 
 
  SELECT * FROM (p_table) INTO TABLE <gt_itab>.
 
 
  READ TABLE <gt_itab> ASSIGNING <gs_entry>
       WITH KEY (p_fld) = '0000000001'.
 
 
END-OF-SELECTION.
7 REPLIES 7
Read only

Former Member
0 Likes
901

Hi,

DATA FCAT1 TYPE LVC_T_FCAT."fieldcat of type internal table

DATA:DYN_ITAB TYPE REF TO DATA,"holding the dynamic internal table

WA TYPE REF TO DATA."holding the wa for dynamic internal table

FIELD-SYMBOLS: <DISP_TABLE> TYPE TABLE,

<WA> TYPE ANY.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = p_tab

CHANGING

CT_FIELDCAT = FCAT1[].

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = FCAT1[]

IMPORTING

EP_TABLE = DYN_ITAB.

ASSIGN DYN_ITAB->* TO <DISP_TABLE>.

CREATE DATA WA LIKE LINE OF <DISP_TABLE>.

ASSIGN WA->* TO <WA>.

SELECT * FROM (DB_TABLE) INTO <WA>.

APPEND <WA> TO <DISP_table>.

ENDSELECT.

rgds,

bharat.

Read only

Former Member
0 Likes
901

see the example program :

REPORT abc.

*----


COMPULSORY

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA : fldname(50) TYPE c.

*----


PARAMETERS : infty(4) TYPE c OBLIGATORY.

DATA : iname LIKE dd02l-tabname.

START-OF-SELECTION.

*----


GET INFO

CONCATENATE 'P' infty INTO iname.

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = iname

TABLES

ddfields = ddfields.

.

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING lt TYPE lvc_t_fcat .

*----


Create Dyn Table From FC

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

*----


Assign Dyn Table To Field Sumbol

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

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

Read only

Former Member
0 Likes
901

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.

DATA: lv_monate TYPE f,
      lv_months TYPE i,
      lv_date TYPE sy-datum.

lv_date = sy-datum + 360.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_check TYPE c.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

  CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
    EXPORTING
      i_datum_bis   = lv_date
      i_datum_von   = sy-datum
      i_kz_incl_bis = ' '
    IMPORTING
      e_monate      = lv_monate.

  lv_months = lv_monate.

  PERFORM build_dyn_itab.
  PERFORM build_report.
  LOOP AT <dyn_table> INTO <dyn_wa>.
    WRITE:/ <dyn_wa>.
  ENDLOOP.
*************************************************************************
FORM build_dyn_itab.
  DATA: index(3) TYPE c.
  DATA: new_table TYPE REF TO data,
  new_line TYPE REF TO data,
  wa_it_fldcat TYPE lvc_s_fcat.
  CLEAR wa_it_fldcat.
  wa_it_fldcat-fieldname = 'AUFNR'.
  wa_it_fldcat-datatype = 'CHAR'.
  wa_it_fldcat-intlen = 12.
  APPEND wa_it_fldcat TO it_fldcat .
  CLEAR wa_it_fldcat.
  wa_it_fldcat-fieldname = 'POSNR'.
  wa_it_fldcat-datatype = 'CHAR'.
  wa_it_fldcat-intlen = 6.
  APPEND wa_it_fldcat TO it_fldcat .
* Create fields
  CLEAR index.
  DO 2 TIMES.
    index = sy-index.
    CLEAR wa_it_fldcat.
    CONCATENATE 'Field' index INTO
    wa_it_fldcat-fieldname .
    CONDENSE wa_it_fldcat-fieldname NO-GAPS.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 5.
    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.                    "build_dyn_itab


**********************************************************************
FORM build_report.
  DATA: fieldname(20) TYPE c.
  DATA: fieldvalue(5) TYPE c.
  DATA: index(3) TYPE c.
  FIELD-SYMBOLS: <fs1>.
  ASSIGN COMPONENT 'AUFNR' OF STRUCTURE <dyn_wa> TO <fs1>.
  <fs1> = '123456789'.
  ASSIGN COMPONENT 'POSNR' OF STRUCTURE <dyn_wa> TO <fs1>.
  <fs1> = '000001'.
  DO 2 TIMES.
    index = sy-index.
* Set up fieldname
    CONCATENATE 'FIELD' index INTO fieldname .
    CONDENSE fieldname NO-GAPS.
* Set up fieldvalue
    CONCATENATE 'FLD' index INTO fieldvalue.
    CONDENSE fieldvalue NO-GAPS.
    ASSIGN COMPONENT fieldname OF STRUCTURE <dyn_wa> TO <fs1>.
    <fs1> = fieldvalue.
  ENDDO.
* Append to the dynamic internal table
  APPEND <dyn_wa> TO <dyn_table>.
ENDFORM.                    "build_report

thx

pavan

Read only

Former Member
0 Likes
902
try this..

 
DATA:
  gdo_data      TYPE REF TO data.
 
FIELD-SYMBOLS:
  <gt_itab>     TYPE table,
  <gs_entry>    TYPE ANY.
 
 
PARAMETERS:
  p_table    TYPE tabname    DEFAULT 'KNA1',
  p_fld      TYPE fieldname  DEFAULT 'KUNNR'.
 
 
START-OF-SELECTION.
 
  CREATE DATA gdo_data TYPE TABLE OF (p_table).
  ASSIGN gdo_data->* TO <gt_itab>.
 
 
  SELECT * FROM (p_table) INTO TABLE <gt_itab>.
 
 
  READ TABLE <gt_itab> ASSIGNING <gs_entry>
       WITH KEY (p_fld) = '0000000001'.
 
 
END-OF-SELECTION.
Read only

Former Member
0 Likes
901

Hi,

Follow the below example.

DATA: i_alv_cat TYPE TABLE OF lvc_s_fcat,

i_dyn_tab TYPE REF TO data.

For this first you need to prepare i_alv_cat in the below way.

for all the fields in your itab you need to prepare i_alv_cat.

w_alv_cat-fieldname = 'VKORG'.

w_alv_cat-ref_table = 'MVKE'.

w_alv_cat-ref_field = 'VKORG'.

APPEND w_alv_cat TO i_alv_cat.

  • Field catalog with the fields the user selected, used as input

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = i_alv_cat

IMPORTING

ep_table = i_dyn_tab.

  • Create ref to the newly generated table <l_table>

  • Plus a new Line with the same structure as the table.

ASSIGN i_dyn_tab->* TO <l_table>.

CREATE DATA w_dyn_line LIKE LINE OF <l_table>.

CREATE DATA w_dyn_line1 LIKE LINE OF <l_table>.

ASSIGN w_dyn_line->* TO <l_line>.

Cheers,

Bujji

Read only

Former Member
0 Likes
901

please go thorugh the link for all types of dynanic coding of retriving of table data for row and header.

[<b>url=http://www.itp-consulting.com/dynamic%20SQL%20in%20SAP.pdf]http://www.itp-consulting.com/dynamic%20SQL%20in%20SAP.pdf[/url]</b>

reward points if it is useful ...

girish

Read only

Former Member