abap

How I display the description of the table as data type of columns?


I have this program to generate data randomly, I need to display the data type of table columns in the output when I generate data, data type have to be next to the field name for example First_Name (CHAR), ID (CHAR). How can I make this please with some explanation because I am still beginner? Thank you.

Report Z_THIRD_REPORT.

PARAMETERS: p_table TYPE string OBLIGATORY,
            p_num   TYPE i DEFAULT 1.

DATA: ls_dd02l   TYPE dd02l,
      lv_message TYPE string.

START-OF-SELECTION.

*DATA(output) = REDUCE char40( INIT out type char40
**                              text = `Count up:`
*                              FOR n = 1 UNTIL n > 11
*                              NEXT out = |{ out }9|
**                              text = |{ n }|
*                               ).
  SELECT  SINGLE
  tabname
  FROM dd02l
  WHERE
  tabname = @p_table
  INTO @DATA(lv_thrash).

  IF sy-subrc NE 0.
    MESSAGE 'Don`t have table!' TYPE 'E'.
  ENDIF.

  DATA(lt_component) = CAST cl_abap_structdescr(  cl_abap_typedescr=>describe_by_name( p_table ) )->get_components( ).

"commit work"

  DATA(tabletype) = cl_abap_tabledescr=>create(
    p_line_type = cl_abap_structdescr=>create( p_components = lt_component )
  ).

  DATA lot TYPE REF TO data.
  CREATE DATA lot  TYPE HANDLE tabletype .

  FIELD-SYMBOLS: <ft> TYPE STANDARD TABLE.

  ASSIGN lot->* TO <ft>.


  DATA lv_max TYPE i.
  DATA lv_ret TYPE string.
  DO p_num TIMES.
    APPEND INITIAL LINE TO <ft> ASSIGNING FIELD-SYMBOL(<ls>).
    LOOP AT lt_component ASSIGNING FIELD-SYMBOL(<ls_comp>).

      ASSIGN COMPONENT <ls_comp>-name OF STRUCTURE <ls> TO FIELD-SYMBOL(<fv>).

      DATA(lv_type) = <ls_comp>-type->get_relative_name(  ).

      DATA(lv_length) = <ls_comp>-type->length / 2.

      CASE <ls_comp>-type->type_kind.
        WHEN 'C'.
          CLEAR lv_ret.
          CALL FUNCTION 'GENERAL_GET_RANDOM_STRING'
            EXPORTING
              number_chars  = lv_length
            IMPORTING
              random_string = lv_ret.
          <fv> = lv_ret.

        WHEN 'D' .

          "also don't want write"


        WHEN 'T' OR 'N' OR 'I'.

          CLEAR lv_max.
          DO lv_length TIMES.
            lv_max = |{ lv_max }9|.

          ENDDO.

          DATA(lo_rand) = cl_abap_random_int=>create( EXPORTING  seed = conv #( sy-timlo ) min = 0 max = lv_max ).
          WAIT UP TO '1' SECONDS.

          <fv> = lo_rand->get_next( ).
           free lo_rand.

        WHEN 'P'.
          "I just don't want write it also)

      ENDCASE.

    ENDLOOP.
  ENDDO.

  cl_demo_output=>write_data(  <ft> ).

  cl_demo_output=>display(  ).

  MODIFY (p_table) FROM TABLE <ft>.

  COMMIT WORK AND WAIT.

Solution

  • I would use SALV table for the display and there you can edit the field names easily. Otherwise you can try other solutions, but this is the first one that came to my mind. If you use this code instead of cl_demo_output=>display( ). it should work. At least it worked fine for me.

      DATA go_salv_table TYPE REF TO cl_salv_table.
      TRY.
          cl_salv_table=>factory(
            IMPORTING
              r_salv_table   =  go_salv_table   " Basisklasse einfache ALV Tabellen
            CHANGING
              t_table        =  <ft> ).
        CATCH cx_salv_msg ##NO_HANDLER.
      ENDTRY.
      DATA(go_columns) = go_salv_table->get_columns( ).
    
      SELECT fieldname, datatype
        FROM dd03l
        INTO TABLE @DATA(gt_dd03l)
        WHERE dd03l~tabname = @lv_thrash
        AND datatype <> ''.
      IF sy-subrc = 0.
        LOOP AT gt_dd03l ASSIGNING FIELD-SYMBOL(<ls_dd03l>).
          DATA(lo_column)  = go_columns->get_column( columnname = <ls_dd03l>-fieldname ).
          lo_column->set_short_text( |{ lo_column->get_short_text( ) }({ <ls_dd03l>-datatype })| ).
          lo_column->set_medium_text( |{ lo_column->get_medium_text( ) }({ <ls_dd03l>-datatype })| ).
          lo_column->set_long_text( |{ lo_column->get_long_text( ) }({ <ls_dd03l>-datatype })| ).
        ENDLOOP.
      ENDIF.
    
      go_salv_table->display( ).
    

    The explanation is simply that you can use several approaches to display, but the best solutions for editing field names are SALV or ALV_GRID. SALV can be generated with the factory method and then a fieldcatalog is automatically created based on the fields in your table. To edit it, you have to query the columns. SELECT will look up all the fields in the table and their corresponding data type, which will be added to the column name in the LOOP section later. Then it is displayed at the end with a display method.

    Here is a good description of SALV: https://blogs.sap.com/2021/06/28/salv-alv-quickstart-snippets-%F0%9F%9A%80/