Home > SAP Tips > ABAP/Java developer tips > Creating dynamic tables in R/3 4.6C
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ABAP/JAVA DEVELOPER TIPS

Creating dynamic tables in R/3 4.6C


David Infantes
06.16.2005
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Looking for a way to create a dynamic internal table? In release WAS 620, you can, by using :

CREATE DATA new_line TYPE table of (p_tab).

This syntax was not allowed in earlier releases. The FM Y_CREATE_DYNAMIC_TABLE can help you to achieve this goal.

Code

FUNCTION y_create_dynamic_table.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(I_STRUCT_NAME) TYPE  DD02L-TABNAME
*"     REFERENCE(I_ALV_CLEAR_KEY) TYPE  FLAG OPTIONAL
*"  EXPORTING
*"     REFERENCE(E_TOTAL_LENGTH) TYPE  I
*"  TABLES
*"      CTBL_ALV_FIELDCAT TYPE  SLIS_T_FIELDCAT_ALV OPTIONAL
*"      CTBL_DYN_CATALOG STRUCTURE  LVC_S_FCAT OPTIONAL
*"  CHANGING
*"     REFERENCE(DYN_WA) TYPE REF TO  DATA
*"     REFERENCE(DYN_TABLE) TYPE REF TO  DATA
*"----------------------------------------------------------------------


  FIELD-SYMBOLS :   <tbl_output> TYPE STANDARD TABLE,
                    <wa_output>  TYPE ANY.

  DATA: new_line  TYPE REF TO data,  " dereference <fs>
        new_table TYPE REF TO data.

* Dynamic field description of a structure
  DATA: ref_descr TYPE REF TO cl_abap_structdescr,
        wa_comp TYPE abap_compdescr.

* cl_alv_table_create=>create_dynamic_table METHOD
  DATA: it_fieldcat TYPE lvc_t_fcat,
        is_fieldcat LIKE LINE OF it_fieldcat.
* REUSE_ALV FM
  DATA: ct_fieldcat TYPE  slis_t_fieldcat_alv,
        wa_fc LIKE LINE OF ct_fieldcat.


** create working area according to selection
  CREATE DATA new_line TYPE (i_struct_name).
  ASSIGN new_line->* TO <wa_output>.

* create table-> fill catalog
  ref_descr ?= cl_abap_typedescr=>describe_by_data( <wa_output> ).

  LOOP AT ref_descr->components INTO wa_comp.

    MOVE:
       wa_comp-name  TO is_fieldcat-fieldname,
       wa_comp-name  TO is_fieldcat-ref_field,
       i_struct_name TO is_fieldcat-ref_table.
    APPEND is_fieldcat TO it_fieldcat.
    ADD wa_comp-length TO e_total_length.
*     Catalog for REUSE_ALV_GRID_DISPLAY
    MOVE-CORRESPONDING is_fieldcat TO wa_fc.

    wa_fc-fieldname   = is_fieldcat-fieldname.
    wa_fc-fieldname   = is_fieldcat-ref_field.
    wa_fc-ref_tabname = is_fieldcat-ref_table.
*   If the key is cleared, the order of columns are not changed
    IF NOT i_alv_clear_key IS INITIAL.
      CLEAR wa_fc-key.
    ENDIF.
    APPEND wa_fc TO ct_fieldcat.
  ENDLOOP.


  CALL METHOD cl_alv_table_create=>create_dynamic_table
   EXPORTING
     it_fieldcatalog = it_fieldcat
   IMPORTING
     ep_table        = new_table.

  ASSIGN new_table->* TO <tbl_output>.

* Return
  ctbl_alv_fieldcat[] = ct_fieldcat[].   " ALV use
  ctbl_dyn_catalog[]  = it_fieldcat[].   " create_dynamic_table reuse
* dyn_table[] = <tbl_output>.  " generates a dump, <fs> not assigned
* dyn_wa = <wa_output>.        " when calling the FM
  dyn_table = new_table.
  dyn_wa = new_line.


ENDFUNCTION.



In the calling program:

*------------
* F I E L D – S Y M B O L S
*------------
FIELD-SYMBOLS : <tbl> TYPE STANDARD TABLE,
                <wa>  TYPE ANY.

*--------------
* D A T A
*--------------
DATA: new_line  TYPE REF TO data,  " dereference <fs>
      new_table TYPE REF TO data.

    CALL FUNCTION 'Y_CREATE_DYNAMIC_TABLE'
         EXPORTING
              i_struct_name     = p_tab
              i_alv_clear_key   = 'X'
         TABLES
              ctbl_alv_fieldcat = ct_fieldcat
         CHANGING
              dyn_wa            = new_line
              dyn_table         = new_table.




* Dereference to field symbol

  ASSIGN new_table->* TO <tb>.
  ASSIGN new_line->* TO <wa>.

Rate this Tip
To rate tips, you must be a member of SearchSAP.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ABAP/Java developer tips
Fixing a common OPEN_FORM and START_FORM error in SAPscript
Select Text fields: Case-insensitive
Finding BADIs you can use
Is this the quickest way to find a BADI?
ABAP Objects in SAP Workflow to provide improved performance
Easily debug error messages in SAP processes
Accessing private attributes in ABAP Objects
Find a BADI in a minute
Top 10 SAP tips of 2007
How to transport an SAP query in R/3 4.6x

SAP ABAP
The difference between an SAP system and a non-SAP system
Is C, C++ and Java knowledge required when learning SAP?
Getting started with a NetWeaver career
How to be a self-taught NetWeaver and ABAP expert
Is SAP BI knowledge advantageous to an ABAP user?
An ABAP user wants to learn XI
Select Text fields: Case-insensitive
Finding BADIs you can use
Is ABAP knowledge necessary for SAP BI?
Quiz: SAP ABAP development
SAP ABAP Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
ABAP  (SearchSAP.com)
ABAP Objects  (SearchSAP.com)
ABAP Workbench  (SearchSAP.com)
BAPI  (SearchSAP.com)
CATT  (SearchSAP.com)
R/3 Repository  (SearchSAP.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsBlogsTipsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2000 - 2008, TechTarget | Read our Privacy Policy
SearchSAP.com is a search service provided by TechTarget and is completely
independent of and not affiliated with SAP AG.
  TechTarget - The IT Media ROI Experts