DB Export Programı

Table Name : İndirilecek veri tabanı tablosunun adı
Text(200)  : WHERE ifadesi eklenebilir, eklenmez ise tüm satırlar indirilir.
Directory :Dosyaların indirileceği klasör
File Name Prefix : Dosya adı için ön ek
Extension : Dosya adının uzantısı
Rowsize : Her bir dosya için satır sayısı



*&---------------------------------------------------------------------*
*& Report  ZTR_DB_EXPORT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztr_db_export.


TYPE-POOLS abap.

*data: gt_master type tty_master.
******Dinamik Tablo Değişkenleri
DATA:   gt_dyntable         TYPE REF TO data .
DATA:   gt_subtable         TYPE REF TO data .
FIELD-SYMBOLS<fs_dyntable> TYPE STANDARD TABLE " main dynm itab
FIELD-SYMBOLS<fs_subtable> TYPE STANDARD TABLE " main dynm itab

******Dinamik Tablo Değişkenleri
CONSTANTS c_msg_id TYPE symsgid VALUE `ZTR_BASIS`.




DATAgv_precheck TYPE c.

PARAMETERSp_dbname TYPE  tabname16 OBLIGATORY MEMORY ID zdb_name .
PARAMETERSp_clause TYPE  char200 MEMORY ID zdb_clause .


* pathintern ----> text60 yapıldı
PARAMETERSp_dir     TYPE  text60 MEMORY ID zdb_dir
                                        OBLIGATORY."target directory
PARAMETERSp_filepr  TYPE  fileintern MEMORY ID zdb_fpr
                                        OBLIGATORY."filename prefix
PARAMETERSp_fileex  TYPE  char4      MEMORY ID zdb_fex
                                        OBLIGATORY."filename extension


PARAMETERSp_rowsze  TYPE int4
                       OBLIGATORY MEMORY ID zdb_rsze  " Satır sayısı



START-OF-SELECTION.
  PERFORM f_check_input CHANGING gv_precheck .

END-OF-SELECTION.

  IF gv_precheck IS INITIAL.
    PERFORM f_save_to_file.
  ENDIF.




*&---------------------------------------------------------------------*
*&      Form  f_check_input
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--CHN_ERR    text
*----------------------------------------------------------------------*
FORM f_check_input CHANGING chn_err TYPE c.
  DATAlv_directory TYPE string.
  DATAlv_fiziksel_dosya_adi TYPE string.
  DATAlv_result TYPE abap_bool.
  CLEAR chn_err .
  DATAlv_new_file_name TYPE fileextern .


  lv_directory p_dir.
  CALL METHOD cl_gui_frontend_services=>directory_exist
    EXPORTING
      directory            lv_directory
    RECEIVING
      result               lv_result
    EXCEPTIONS
      cntl_error           1
      error_no_gui         2
      wrong_parameter      3
      not_supported_by_gui 4
      OTHERS               5.
  IF lv_result IS INITIAL.
    chn_err `X`."Dosya klasörü bulunmadı
    PERFORM f_show_message_vl_1 USING `002` lv_directory.
  ENDIF.


  CONCATENATE  p_filepr `.` p_fileex INTO lv_new_file_name .
  CONCATENATE lv_directory  lv_new_file_name INTO
lv_fiziksel_dosya_adi .

  DATAls_dd02l TYPE dd02l.

  SELECT SINGLE INTO ls_dd02l FROM dd02l  WHERE tabname p_dbname
    AND as4local `A`
    AND  NOT tabclass `APPEND` OR tabclass `INTTAB` )  .
  IF sy-subrc NE 0.
    chn_err `X`."Veritabanı tablosu bulunamadı :&
    PERFORM f_show_message_vl_1 USING `004` p_dbname.
  ENDIF.

*    TRANSP  Transparent table
*    INTTAB  Structure
*    CLUSTER  Cluster table
*    POOL  Pooled table
*    VIEW  General view structure
*    APPEND  Append structure


ENDFORM.                    "f_check_input



*&---------------------------------------------------------------------*
*&      Form  f_show_message
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PRM_TITLE  text
*      -->PRM_MSGID  text
*      -->PRM_MSGTY  text
*      -->PRM_MSGNO  text
*      -->PRM_MSGV1  text
*      -->PRM_MSGV2  text
*      -->PRM_MSGV3  text
*      -->PRM_MSGV4  text
*----------------------------------------------------------------------*
FORM f_show_message USING prm_title prm_msgid prm_msgty prm_msgno
                          prm_msgv1 prm_msgv2 prm_msgv3 prm_msgv4.
  DATAlv_title TYPE string,
        lv_msgid TYPE  symsgid,
        lv_msgty TYPE  symsgty,
        lv_msgno TYPE  symsgno,
        lv_msgv1 TYPE  symsgv,
        lv_msgv2 TYPE  symsgv,
        lv_msgv3 TYPE  symsgv,
        lv_msgv4 TYPE  symsgv.


  lv_title prm_title.
  lv_msgid prm_msgid.
  lv_msgty prm_msgty.
  lv_msgno prm_msgno.
  lv_msgv1 prm_msgv1.
  lv_msgv2 prm_msgv2.
  lv_msgv3 prm_msgv3.
  lv_msgv4 prm_msgv4.

  CALL FUNCTION `POPUP_DISPLAY_MESSAGE`
    EXPORTING
      titel lv_title
      msgid lv_msgid
      msgty lv_msgty
      msgno lv_msgno
      msgv1 lv_msgv1
      msgv2 lv_msgv2
      msgv3 lv_msgv3
      msgv4 lv_msgv4.
ENDFORM.                    "f_show_message


*&---------------------------------------------------------------------*
*&      Form  f_show_message_vl_0
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PRM_MSGNO  text
*----------------------------------------------------------------------*
FORM f_show_message_vl_0 USING prm_msgno .
  PERFORM f_show_message USING text-err c_msg_id `E` prm_msgno
                               ``       ``        ``  ``.
ENDFORM.                    "f_show_message_vl_0
*&---------------------------------------------------------------------*
*&      Form  f_show_message_vl_1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PRM_MSGNO  text
*      -->PRM_MSGV1  text
*----------------------------------------------------------------------*
FORM f_show_message_vl_1 USING prm_msgno prm_msgv1.
  PERFORM f_show_message USING text-err c_msg_id `E` prm_msgno
                               prm_msgv1       ``        ``  ``.
ENDFORM.                    "f_show_message_vl_1

*&---------------------------------------------------------------------*
*&      Form  f_show_message_vl_2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PRM_MSGNO  text
*      -->PRM_MSGV1  text
*      -->PRM_MSGV2  text
*----------------------------------------------------------------------*
FORM f_show_message_vl_2 USING prm_msgno prm_msgv1 prm_msgv2.
  PERFORM f_show_message USING text-err   c_msg_id `E` prm_msgno
                               prm_msgv1  prm_msgv2        ``  ``.
ENDFORM.                    "f_show_message_vl_2




*&---------------------------------------------------------------------*
*&      Form  f_save_to_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_save_to_file.
*  data: lt_fieldcat_master type lvc_t_fcat.
  PERFORM f_create_dynamic_table USING    p_dbname .
  PERFORM f_fill_table   .
  PERFORM f_save_table   .

  IF <fs_dyntable> IS  ASSIGNED.
    REFRESH <fs_dyntable>.
    UNASSIGN <fs_dyntable>.
  ENDIF.

  IF <fs_subtable> IS  ASSIGNED.
    REFRESH <fs_subtable>.
    UNASSIGN <fs_subtable>.
  ENDIF.

ENDFORM.                    "f_save_to_file


*&---------------------------------------------------------------------*
*&      Form  f_create_dynamic_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PRM_TABNAME  text
*----------------------------------------------------------------------*
FORM f_create_dynamic_table  USING  prm_tabname    TYPE tabname16.

  DATAlv_structure TYPE dd02l-tabname .

  CREATE DATA gt_dyntable TYPE STANDARD TABLE OF (prm_tabname).
  ASSIGN gt_dyntable->TO <fs_dyntable>.

  CREATE DATA gt_subtable TYPE STANDARD TABLE OF (prm_tabname).
  ASSIGN gt_subtable->TO <fs_subtable>.



ENDFORM.                    "f_create_dynamic_table





*&---------------------------------------------------------------------*
*&      Form  f_fill_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_fill_table   .
  DATAlv_where_clause TYPE string.
  CHECK <fs_dyntable> IS ASSIGNED.
  IF p_clause IS INITIAL.
    SELECT INTO TABLE <fs_dyntable> FROM (p_dbname).
  ELSE.
    lv_where_clause =  p_clause .
    SELECT INTO TABLE <fs_dyntable> FROM (p_dbname)
      WHERE (lv_where_clause).
  ENDIF.
ENDFORM.                    "



*&---------------------------------------------------------------------*
*&      Form  f_save_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_save_table.
  DATAlv_count TYPE int4.
  DATAlv_total_count TYPE int4.
  DATAlv_file TYPE string.
  DATAlv_sayac TYPE numc3.
  DATAlv_xndx TYPE int4.

  FIELD-SYMBOLS<fs_row> TYPE ANY.

  DESCRIBE TABLE <fs_dyntable> LINES   lv_total_count.
  lv_sayac 0.
  LOOP AT <fs_dyntable> ASSIGNING   <fs_row>.
    lv_xndx sy-tabix.
    APPEND <fs_row> TO <fs_subtable> .
    DESCRIBE TABLE <fs_subtable> LINES  lv_count.

    IF lv_count p_rowsze OR lv_xndx lv_total_count .
      lv_sayac lv_sayac + 1.

      CONCATENATE p_dir p_filepr `_` lv_sayac `.` p_fileex
                 INTO  lv_file.

      CALL METHOD cl_gui_frontend_services=>gui_download
        EXPORTING
          filename lv_file
          filetype `DAT`
        CHANGING
          data_tab <fs_subtable>[].
      WRITE:/ lv_file.

      REFRESH <fs_subtable>.
    ENDIF.

  ENDLOOP.

ENDFORM.                    "f_save_table