Source code of program: ZPJ_EXPORT_TABLE

Description: Export Content of DB Table

*&---------------------------------------------------------------------*
*& Report  ZPJ_EXPORT_TABLE
*&
*&---------------------------------------------------------------------*
*& Pavel Jaroš, 22.10.2013
*& Export velkých tabulek (typicky BSEG)
*& Parametry:
*& 1) p_table - DB tabulka, napr. BSEG
*& 2) p_struc - struktura definuje, ktera pole chci exportovat, pokud
*&              nechci exportovat celou tabulku, napr. ZEXPORT_BSEG
*&---------------------------------------------------------------------*

REPORT  zpj_export_table.

****************************************
* DECLARATIONS                         *
****************************************
TYPE-POOLS truxs.

CONSTANTS: c_separator TYPE c VALUE ';'.

FIELD-SYMBOLS: <xtab> TYPE STANDARD TABLE.
DATA: g_header(4096) TYPE c.

****************************************
* SELECTION SCREEN                     *
****************************************
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_table TYPE tabname OBLIGATORY,
            p_struc TYPE tabname,
            p_fpath TYPE ibipparms-path DEFAULT 'C:\temp\' OBLIGATORY
             LOWER CASE,
            p_mrows(10) TYPE n.
SELECTION-SCREEN: END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fpath.
  PERFORM f4_folder CHANGING p_fpath.

****************************************
* AT SELECTION-SCREEN                  *
****************************************
AT SELECTION-SCREEN.
  PERFORM evt_at_selscreen.

****************************************
* START OF SELECTION                   *
****************************************
START-OF-SELECTION.
  PERFORM add_back_slash CHANGING p_fpath.
  PERFORM export_table.

*&---------------------------------------------------------------------*
*&      Form  evt_at_selscreen
*&---------------------------------------------------------------------*
FORM evt_at_selscreen.
  DATA: l_tabclass LIKE dd02l-tabclass.

* global field check on F8 key
  CHECK sy-ucomm = 'ONLI'.

* kontrola tabulky
  SELECT SINGLE tabclass FROM dd02l INTO l_tabclass
  WHERE tabname = p_table.

  IF sy-subrc <> 0.
    MESSAGE e000(xt) WITH  'DB tabulka' p_table 'neexistuje!'.
  ELSEIF l_tabclass = 'INTTAB'.
    MESSAGE e000(xt) WITH  p_table 'není DB tabulka, ale struktura!'.
  ENDIF.

  IF NOT p_struc IS INITIAL.
*   kontrola struktury
    SELECT SINGLE tabclass FROM dd02l INTO l_tabclass
    WHERE tabname = p_struc.

    IF sy-subrc <> 0.
      MESSAGE e000(xt) WITH  'Struktura' p_struc 'neexistuje!'.
    ENDIF.
  ENDIF.
ENDFORM.                    "evt_at_selscreen

*&---------------------------------------------------------------------*
*&      Form  export_table
*&---------------------------------------------------------------------*
* source: http://wiki.scn.sap.com/wiki/display/ABAP/
* How+to+Select+data+from+large+tables
*&---------------------------------------------------------------------*
FORM export_table.
  DATA: l_db_cursor TYPE cursor,
        lt_dfies TYPE TABLE OF dfies,
        ls_dfies TYPE dfies,
        l_struc_size TYPE i VALUE 0,
        l_package_size TYPE i,
        l_refitab TYPE REF TO data,
        l_num(3) TYPE n,
        l_mrows TYPE i.

* If no output structure is defined, the structure of DB table is used
  IF p_struc IS INITIAL.
    p_struc = p_table.
  ENDIF.

  CREATE DATA l_refitab TYPE TABLE OF (p_struc).
  ASSIGN l_refitab->* TO <xtab>.
* get nametab
  CALL FUNCTION 'DDIF_NAMETAB_GET'
    EXPORTING
      tabname   = p_struc
    TABLES
      dfies_tab = lt_dfies
    EXCEPTIONS
      not_found = 1
      OTHERS    = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.
** Logic for calculating Package size
* To calculate the memory taken by one record
  LOOP AT lt_dfies INTO ls_dfies.
    l_struc_size = l_struc_size + ls_dfies-leng.
  ENDLOOP.

***** To calculaten maximum no of records that can be accomodated in 2 GB
*  L_PACKAGE_SIZE = 2147483648 / L_STRUC_SIZE.

  l_package_size = 1073741824 / l_struc_size. "1 GB

  PERFORM prepare_header.

  IF p_mrows > 0.
    l_mrows = p_mrows.
  ENDIF.

  OPEN CURSOR WITH HOLD l_db_cursor FOR SELECT * FROM (p_table)
  UP TO l_mrows ROWS
  BYPASSING BUFFER.
  DO.
    l_num = sy-index.
*** To Fetch data in chunks of 1 GB
    FETCH NEXT CURSOR l_db_cursor
    INTO CORRESPONDING FIELDS OF TABLE <xtab>
    PACKAGE SIZE l_package_size.
    IF sy-subrc NE 0.
      CLOSE CURSOR l_db_cursor.
      EXIT.
    ENDIF.

    PERFORM export_file USING l_num.
  ENDDO.
ENDFORM.                    "export_table

*&---------------------------------------------------------------------*
*&      Form  prepare_header
*&---------------------------------------------------------------------*
FORM prepare_header.
  DATA: lt_dfies TYPE dfies OCCURS 100 WITH HEADER LINE.

  CALL FUNCTION 'DDIF_FIELDINFO_GET'
    EXPORTING
      tabname   = p_struc
      langu     = sy-langu
    TABLES
      dfies_tab = lt_dfies.

  LOOP AT lt_dfies.
    IF lt_dfies-scrtext_l IS INITIAL.
      lt_dfies-scrtext_l = lt_dfies-fieldname.
    ENDIF.
    IF sy-tabix = 1.
      g_header = lt_dfies-scrtext_l.
    ELSE.
      CONCATENATE g_header lt_dfies-scrtext_l
      INTO g_header SEPARATED BY c_separator.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "prepare_header

*&---------------------------------------------------------------------*
*&      Form  export_file
*&---------------------------------------------------------------------*
FORM export_file USING p_num.
  DATA:
    lv_exp_file   TYPE string,
    lt_tex_tab    TYPE truxs_t_text_data.

  CONCATENATE p_fpath p_table '_' p_num '.csv' INTO lv_exp_file.

* kopie funkčního modulu SAP_CONVERT_TO_TEX_FORMAT
* přidána oprava, která ošetřuje ostatní datové typy: I, s...
**  CASE l_type.
**    WHEN ...
**    WHEN 'I' OR 's'.
**      l_date_extern = <f_source>.
**      ASSIGN l_date_extern TO <f_source>.
**    WHEN OTHERS.
**  ENDCASE.

  CALL FUNCTION 'Z_CONVERT_TO_TEXT'
    EXPORTING
      i_field_seperator    = c_separator
    TABLES
      i_tab_sap_data       = <xtab>
    CHANGING
      i_tab_converted_data = lt_tex_tab.

  INSERT g_header INTO lt_tex_tab INDEX 1.

  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename = lv_exp_file
      filetype = 'DAT'
*      codepage = '4310'
    CHANGING
      data_tab = lt_tex_tab
    EXCEPTIONS
      OTHERS   = 4.
ENDFORM.                    "export_file

*&---------------------------------------------------------------------*
*&      Form  f4_folder
*&---------------------------------------------------------------------*
FORM f4_folder CHANGING p_path TYPE c.
  DATA: l_folder TYPE string,
        l_title TYPE string.

  l_folder = p_path.
  l_title = 'Choose folder'(fld).

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title    = l_title
      initial_folder  = l_folder
    CHANGING
      selected_folder = l_folder.

  p_path = l_folder.
  PERFORM add_back_slash CHANGING p_path.
ENDFORM.                                                    "f4_folder

*&---------------------------------------------------------------------*
*&      Form  add_back_slash
*&---------------------------------------------------------------------*
FORM add_back_slash CHANGING pc_file TYPE c.
  CONSTANTS lc_bslash TYPE c VALUE '\'.
  DATA l_len TYPE i.

  CHECK NOT pc_file IS INITIAL.

  l_len = STRLEN( pc_file ) - 1.
  IF pc_file+l_len(1) <> lc_bslash.
    CONCATENATE pc_file lc_bslash INTO pc_file.
  ENDIF.
ENDFORM.                    "add_back_slash

*Text elements
*----------------------------------------------------------
* 001 Selection Criteria


*Selection texts
*----------------------------------------------------------
* P_FPATH         Output folder
* P_MROWS         Maximum No. of Hits
* P_STRUC         Output structure
* P_TABLE         Source DB table


*Messages
*----------------------------------------------------------
*
* Message class: XT
*000   &1 &2 &3 &4