Friday, 7 April 2017

Field Symbols

Field Symbols when used as Work area for the Internal Table:
*&---------------------------------------------------------------------*
*& Report  ZDEMO
*&
*&---------------------------------------------------------------------*
*& Field Symbols Example
*&
*&---------------------------------------------------------------------*
REPORT zdemo.

INITIALIZATION.
  TYPES: BEGIN OF line,
           col1 TYPE c,
           col2 TYPE c,
         END OF line.

  DATA: gt_tab      TYPE TABLE OF line,
        gv_key      TYPE char4 VALUE 'COL1',
        gv_data_ref TYPE REF TO data
        .

  FIELD-SYMBOLS: <fs_gt_tab> TYPE ANY TABLE,
                 <fs_gs_tab> LIKE LINE OF gt_tab
                 .

START-OF-SELECTION.
  CREATE DATA gv_data_ref LIKE LINE OF gt_tab.
  ASSIGN gv_data_ref->* TO <fs_gs_tab>.
  ASSIGN gt_tab TO <fs_gt_tab>.

  <fs_gs_tab>-col1 = 'A'.
  <fs_gs_tab>-col2 = '1'.
  APPEND <fs_gs_tab> TO gt_tab.

  <fs_gs_tab>-col1 = 'B'.
  <fs_gs_tab>-col2 = '2'.
  APPEND <fs_gs_tab> TO gt_tab.

  READ TABLE <fs_gt_tab> WITH KEY ('COL1') = 'A' ASSIGNING <fs_gs_tab>.

  WRITE: / 'Before: ', <fs_gs_tab>-col2.
  <fs_gs_tab>-col2 = 'I'.
  WRITE: / 'After: ', <fs_gs_tab>-col2.

NOTE: The parenthesis '( )' means that the value will be determined in the run time.


Example 2: Display table data according to the name given by the user
*"DISPLAY TABLE DATA ACCORDING TO THE TABLE NAME GIVEN BY USER
*
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS:  p_tab TYPE tabname OBLIGATORY.
PARAMETERS:  p_no TYPE i.
SELECTION-SCREEN: END OF BLOCK b1.

AT SELECTION-SCREEN.
  SELECT COUNT(*) FROM dd03l WHERE tabname = p_tab.
  IF sy-subrc NE 0.
    MESSAGE 'Invalid Table Name' TYPE 'E'.
  ENDIF.

START-OF-SELECTION.

  DATA: lv_dref TYPE REF TO data.
  FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE,
                 <fs_tmp> TYPE any
                 .

  CREATE DATA lv_dref TYPE TABLE OF (p_tab).
  ASSIGN lv_dref->* TO <fs_tab>.

  SELECT *
    FROM (p_tab)
    INTO TABLE <fs_tab>
    UP TO p_no ROWS.

  DATA: lt_fcat   TYPE slis_t_fieldcat_alv,
        ls_fcat   TYPE slis_fieldcat_alv,
        ls_layout TYPE slis_layout_alv.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-repid
      i_structure_name       = p_tab
    CHANGING
      ct_fieldcat            = lt_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.

  ls_layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = ls_layout
      it_fieldcat        = lt_fcat
    TABLES
      t_outtab           = <fs_tab>
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
*     Implement suitable error handling here
  ENDIF.

No comments:

Post a Comment

Report to find CDS view of Standard Table

A small change has been made to the original program ( SAP YARD Article ) so that it can also display the common CDS used by multiple table...