Showing posts with label Dynamic Internal Table. Show all posts
Showing posts with label Dynamic Internal Table. Show all posts

Jan 25, 2014

Display data dynamically using Field Symbols

1.     Introduction: - Displaying the table contents dynamically as per table name mentioned on selection screen with help of field symbols.  If user enters MARA, then the data from MARA table is retrieved. If VBAK, then VBAK data is shown.  

2.     Steps:- 
Declare two parameter variables as below:-
Create a field symbol as below:-
Initially check whether table exists or not.
If table exists in database then by using create data statement create the data object and assign the reference to data object of w_dref reference variable.
Now assign the reference variable to field symbol as below:
In runtime after assign statement the <t_itab> structure will be same as table name entered on selection screen.
Now select the data from database and put it in field-symbol (dynamic internal table).
Now create the field catalog by using the function module .
Display the data by using function module  as below:-
  
Example:-
Give table name as mara on selection screen and no of records as 100 and press F8. 
List is displayed as below:
Now give table name as VBAK and press F8.

3.     Summary:-
The importance of this tutorial is to display the data as per table name mentioned on selection screen by user with Field symbols concept.
a)     First declare the two parameters one for table name and other for no of records to be displayed.
b)    Declare the field symbol of type standard table.
c)     Check table name given on selection screen exists or not.
d)    Create the internal table at runtime and select the data from table and place it in dynamic internal table (Field symbol).
e)     Create the field catalog and display the data.  
4.     Source Code:-
REPORT y_saptechnical_dynamic NO STANDARD PAGE HEADING.
*"Type-pool............................................................
TYPE-POOLS : slis.*"Parameters elements..................................................
PARAMETERS : p_table TYPE tabname OBLIGATORY,       " Table Name
             p_no    
TYPE i .                       " No of Records to be displayed

DATA:
  w_dref    
TYPE REF TO data,          " w_dref reference variable
  t_line    
TYPE c LENGTH 20.          " w_line to hold a line
DATA :
  t_fcat  
TYPE slis_t_fieldcat_alv.

FIELD-SYMBOLS: <t_itab> 
TYPE STANDARD TABLE.

START-OF-SELECTION.
  
SELECT SINGLE
         tabname                       
" Table Name
    
FROM dd02l
    
INTO t_line
   
WHERE tabname   EQ p_table
     
AND as4vers   EQ ' '
     
AND as4local  EQ 'A'
     
AND tabclass  NE 'INTTAB'
     
AND tabclass  NE 'APPEND'.

  
IF sy-subrc EQ 0.
    
CREATE DATA w_dref TYPE STANDARD TABLE OF (p_table).
    
ASSIGN w_dref->* TO <t_itab>.
    
IF sy-subrc EQ 0.
      
SELECT *                         " All Fields
        
FROM (p_table)
        
INTO TABLE <t_itab> UP TO p_no ROWS.

      
IF sy-subrc EQ 0.
        
PERFORM fill_catalog.
        
PERFORM display.
      ELSE.
        
MESSAGE text-002 TYPE 'S'.
        EXIT.
      ENDIF.                           
" IF sy-subrc EQ 0...
    ELSE.
      
MESSAGE text-003 TYPE 'S'.
      EXIT.
    ENDIF.
  ELSE.
    
MESSAGE text-003 TYPE 'S'.
    
LEAVE LIST-PROCESSING.
  ENDIF.                               
" IF sy-subrc EQ 0...
FORM display .
  
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    
EXPORTING
      it_fieldcat   = t_fcat
    
TABLES
      t_outtab      = <t_itab>
    
EXCEPTIONS
      program_error = 
1
      
OTHERS        = 2.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno DISPLAY LIKE 'E'
            
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.                               
" IF sy-subrc <> 0...
ENDFORM.                               
" DISPLAY
FORM fill_catalog .
  
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    
EXPORTING
      i_program_name         = sy-repid
      i_structure_name       = p_table
    
CHANGING
      ct_fieldcat            = t_fcat
    
EXCEPTIONS
      inconsistent_interface = 
1
      program_error          = 
2
      
OTHERS                 = 3.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno DISPLAY LIKE 'E'
            
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.                               
" IF sy-subrc <> 0...
ENDFORM.                               
" FILL_CATALOG



Source:
http://saptechnical.com/Tutorials/ABAP/FS/Index.htm
by Vineesh Batchu, Yash Technologies



Nov 28, 2011

Dynamic Internal Table or Array

A sample code how to create a Dynamic Internal Table or Array using Class CL_ALV_TABLE_CREATE


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 'T001'.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
  PERFORM get_structure.
  PERFORM create_dynamic_itab.

*******Creates a dynamic internal table*********
  PERFORM get_data.
  PERFORM write_out.

*&---------------------------------------------------------------------*
*&      Form  get_structure
*&---------------------------------------------------------------------*
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 .
    CASE xdetails-type_kind.
      WHEN 'C'.
        xfc-datatype = 'CHAR'.
      WHEN 'N'.
        xfc-datatype = 'NUMC'.
      WHEN 'D'.
        xfc-datatype = 'DATE'.
      WHEN 'P'.
        xfc-datatype = 'PACK'.
      WHEN OTHERS.
        xfc-datatype = xdetails-type_kind.
    ENDCASE.
    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
*&---------------------------------------------------------------------*
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
      i_length_in_byte = 'X' "added by Paul Robert Oct 28, 2009 17:04
    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
*&---------------------------------------------------------------------*
FORM get_data.
* Select Data from table.
  SELECT * INTO TABLE <dyn_table>
           FROM (p_table).
ENDFORM.                    "get_data

*&---------------------------------------------------------------------*
*&      Form  write_out
*&---------------------------------------------------------------------*
FORM write_out.
  LOOP AT <dyn_table> INTO <dyn_wa>.
    DO.
      ASSIGN COMPONENT  sy-index
         OF STRUCTURE <dyn_wa> TO <dyn_field>.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      IF sy-index = 1.
        WRITE:/ <dyn_field>.
      ELSE.
        WRITE: <dyn_field>.
      ENDIF.
    ENDDO.
  ENDLOOP.
ENDFORM.                    "write_out