Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

sample code for writing extract program

0 Kudos
393

please provide sample code for writing extract program i.e to extract data from database into flat file format with tab delimiter.i had already written bdc code for purchase contract me 31k. presently we are working on ecc6.0 version. i need to extract data from 4.6 c and store data in flat file format .

3 REPLIES 3

Former Member
0 Kudos
81
Hi, I have given the important routines you would need. You could you use this as reference.. you would have to build the rest . Please let me know if you are stuck ..

TABLES: CSKS.                          "Cost center master


************************************************************************
* Data Declaration
************************************************************************
DATA: BEGIN OF T_CSKS OCCURS 0,
         KOKRS LIKE CSKS-KOKRS,        "controlling area
         KOSTL LIKE CSKS-KOSTL,        "Cost center
         DATBI LIKE CSKS-DATBI,        "Valid to date
         DATAB LIKE CSKS-DATAB,        "Date valid from
         GSBER LIKE CSKS-GSBER,        "Business area
         BUKRS LIKE CSKS-BUKRS,        "Company code
         LOGSYSTEM LIKE CSKS-LOGSYSTEM,"Logical System
         MANDT LIKE SY-MANDT,          "client
         WAERS LIKE CSKS-WAERS,        "Currency
         PRCTR LIKE CSKS-PRCTR,        "Profit Centre
         VERAK LIKE CSKS-VERAK,        "Person in charge of cost Center
         RET_LOC(15),                  "Retail Location
      END OF T_CSKS.

*Download String data
DATA: BEGIN OF T_CSKS_X OCCURS 0,
         KOKRS(4), " like csks-kokrs,        "controlling area
         KOSTL(10), " like csks-kostl,        "Cost center
         LOGSYSTEM(3), " like csks-logsystem,"Logical System
          MANDT(3), " like sy-mandt,          "client
         DATBI(8), " like csks-datbi,        "Valid to date
         DATAB(8), " like csks-datab,        "Date valid from
         GSBER(4), " like csks-gsber,        "Business area
         BUKRS(4), " like csks-bukrs,        "Company code
         prctr(10), " like csks-prctr,        "Profit Centre
       waers(5), " like csks-waers,        "Currency
       verak(20), "like csks-verak,  "Person in charge of cost Center
        ret_loc(15),                  "Retail Location
      END OF T_CSKS_X.


************************************************************************
*  Start-of-Selection.                                                 *
************************************************************************
START-OF-SELECTION.
*collect data from csks
  PERFORM COLLECT_DATA.

*transfer to download tab.
  PERFORM DATA_TRANSFER.

*local
  IF P_PCFILE = C_YES.
    PERFORM LOCAL_DOWNLOAD.
  ENDIF.

*unix
  IF P_UNIX = C_YES.
    PERFORM UNIX_DOWNLOAD.
  ENDIF.

  IF P_PCFILE IS INITIAL AND P_UNIX IS INITIAL.
    MESSAGE  WITH 'Down load flags both are unchecked'.
  ENDIF.

END-OF-SELECTION.
 WRITE 😕 'PC File'  , C_UNDER, P_PCFIL.
* clear : p_pcfil, p_unfil.
*&---------------------------------------------------------------------*
*&      Form  COLLECT_DATA
*&---------------------------------------------------------------------*
*       Collect Data                                                   *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM COLLECT_DATA.
* Query from csks table.
  SELECT KOKRS        "Controlling area
         KOSTL        "Cost center
         DATBI        "Valid To Date
         DATAB        "Date Valid From
         GSBER        "Business area
         BUKRS        "Company code
         LOGSYSTEM    "Logical System
         WAERS        "Currency
         PRCTR        "Profit Centre
         INTO CORRESPONDING FIELDS OF
         TABLE T_CSKS
         FROM CSKS.
  W_PCFIL = P_PCFIL.
ENDFORM.                               " COLLECT_DATA

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD
*&---------------------------------------------------------------------*
*       Download                                                       *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DOWNLOAD.
  P_PCFIL =  W_PATH.
  CALL FUNCTION 'WS_DOWNLOAD'
       EXPORTING
           FILENAME            =  P_PCFIL
*           filetype            =  c_dat   "dat
           FILETYPE            =  'ASC' "c_dat   "dat
       TABLES
*            data_tab            = t_str
            DATA_TAB            = T_CSKS_X
*            fieldnames          = t_strhd
       EXCEPTIONS
            FILE_OPEN_ERROR     = 1
            FILE_WRITE_ERROR    = 2
            INVALID_FILESIZE    = 3
            INVALID_TABLE_WIDTH = 4
            INVALID_TYPE        = 5
            NO_BATCH            = 6
            UNKNOWN_ERROR       = 7
            OTHERS              = 8.
  IF SY-SUBRC EQ 0.
  ENDIF.

ENDFORM.                               " DOWNLOAD

*&---------------------------------------------------------------------*
*&      Form  WRITE_TO_SERVER
*&---------------------------------------------------------------------*
*      Write to server                                                 *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM WRITE_TO_SERVER.
  DATA : L_MSG(100) TYPE C,
         L_LINE(5000) TYPE C.
  P_UNFIL =  W_UPATH.

  OPEN DATASET P_UNFIL FOR OUTPUT IN TEXT MODE.        " message l_msg.

  IF SY-SUBRC <> 0.
    WRITE: / L_MSG.
  ENDIF.

*  loop at t_str.
  LOOP AT T_CSKS_X.
    TRANSFER T_CSKS_X TO P_UNFIL.
  ENDLOOP.
  CLOSE DATASET P_UNFIL.
  WRITE : / C_TEXT , W_UPATH.
  SPLIT W_UNIXFILE AT C_DOT INTO W_UNIXFIL1 W_UEXT.
  CLEAR W_UPATH.
  IF NOT W_UEXT IS INITIAL.
    CONCATENATE W_UNIXFIL1  C_DOT W_UEXT INTO W_UPATH.
  ELSE.
    W_UEXT = C_CSV.
    CONCATENATE W_UNIXFIL1 C_DOT W_UEXT INTO W_UPATH.
  ENDIF.


ENDFORM.                               " WRITE_TO_SERVER
[code]

[/code]

Former Member
0 Kudos
81
Hi, I have given the important routines you would need. You could you use this as reference.. you would have to build the rest . Please let me know if you are stuck ..

TABLES: CSKS.                          "Cost center master


************************************************************************
* Data Declaration
************************************************************************
DATA: BEGIN OF T_CSKS OCCURS 0,
         KOKRS LIKE CSKS-KOKRS,        "controlling area
         KOSTL LIKE CSKS-KOSTL,        "Cost center
         DATBI LIKE CSKS-DATBI,        "Valid to date
         DATAB LIKE CSKS-DATAB,        "Date valid from
         GSBER LIKE CSKS-GSBER,        "Business area
         BUKRS LIKE CSKS-BUKRS,        "Company code
         LOGSYSTEM LIKE CSKS-LOGSYSTEM,"Logical System
         MANDT LIKE SY-MANDT,          "client
         WAERS LIKE CSKS-WAERS,        "Currency
         PRCTR LIKE CSKS-PRCTR,        "Profit Centre
         VERAK LIKE CSKS-VERAK,        "Person in charge of cost Center
         RET_LOC(15),                  "Retail Location
      END OF T_CSKS.

*Download String data
DATA: BEGIN OF T_CSKS_X OCCURS 0,
         KOKRS(4), " like csks-kokrs,        "controlling area
         KOSTL(10), " like csks-kostl,        "Cost center
         LOGSYSTEM(3), " like csks-logsystem,"Logical System
          MANDT(3), " like sy-mandt,          "client
         DATBI(8), " like csks-datbi,        "Valid to date
         DATAB(8), " like csks-datab,        "Date valid from
         GSBER(4), " like csks-gsber,        "Business area
         BUKRS(4), " like csks-bukrs,        "Company code
         prctr(10), " like csks-prctr,        "Profit Centre
       waers(5), " like csks-waers,        "Currency
       verak(20), "like csks-verak,  "Person in charge of cost Center
        ret_loc(15),                  "Retail Location
      END OF T_CSKS_X.


************************************************************************
*  Start-of-Selection.                                                 *
************************************************************************
START-OF-SELECTION.
*collect data from csks
  PERFORM COLLECT_DATA.

*transfer to download tab.
  PERFORM DATA_TRANSFER.

*local
  IF P_PCFILE = C_YES.
    PERFORM LOCAL_DOWNLOAD.
  ENDIF.

*unix
  IF P_UNIX = C_YES.
    PERFORM UNIX_DOWNLOAD.
  ENDIF.

  IF P_PCFILE IS INITIAL AND P_UNIX IS INITIAL.
    MESSAGE  WITH 'Down load flags both are unchecked'.
  ENDIF.

END-OF-SELECTION.
 WRITE 😕 'PC File'  , C_UNDER, P_PCFIL.
* clear : p_pcfil, p_unfil.
*&---------------------------------------------------------------------*
*&      Form  COLLECT_DATA
*&---------------------------------------------------------------------*
*       Collect Data                                                   *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM COLLECT_DATA.
* Query from csks table.
  SELECT KOKRS        "Controlling area
         KOSTL        "Cost center
         DATBI        "Valid To Date
         DATAB        "Date Valid From
         GSBER        "Business area
         BUKRS        "Company code
         LOGSYSTEM    "Logical System
         WAERS        "Currency
         PRCTR        "Profit Centre
         INTO CORRESPONDING FIELDS OF
         TABLE T_CSKS
         FROM CSKS.
  W_PCFIL = P_PCFIL.
ENDFORM.                               " COLLECT_DATA

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD
*&---------------------------------------------------------------------*
*       Download                                                       *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DOWNLOAD.
  P_PCFIL =  W_PATH.
  CALL FUNCTION 'WS_DOWNLOAD'
       EXPORTING
           FILENAME            =  P_PCFIL
*           filetype            =  c_dat   "dat
           FILETYPE            =  'ASC' "c_dat   "dat
       TABLES
*            data_tab            = t_str
            DATA_TAB            = T_CSKS_X
*            fieldnames          = t_strhd
       EXCEPTIONS
            FILE_OPEN_ERROR     = 1
            FILE_WRITE_ERROR    = 2
            INVALID_FILESIZE    = 3
            INVALID_TABLE_WIDTH = 4
            INVALID_TYPE        = 5
            NO_BATCH            = 6
            UNKNOWN_ERROR       = 7
            OTHERS              = 8.
  IF SY-SUBRC EQ 0.
  ENDIF.

ENDFORM.                               " DOWNLOAD

*&---------------------------------------------------------------------*
*&      Form  WRITE_TO_SERVER
*&---------------------------------------------------------------------*
*      Write to server                                                 *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM WRITE_TO_SERVER.
  DATA : L_MSG(100) TYPE C,
         L_LINE(5000) TYPE C.
  P_UNFIL =  W_UPATH.

  OPEN DATASET P_UNFIL FOR OUTPUT IN TEXT MODE.        " message l_msg.

  IF SY-SUBRC <> 0.
    WRITE: / L_MSG.
  ENDIF.

*  loop at t_str.
  LOOP AT T_CSKS_X.
    TRANSFER T_CSKS_X TO P_UNFIL.
  ENDLOOP.
  CLOSE DATASET P_UNFIL.
  WRITE : / C_TEXT , W_UPATH.
  SPLIT W_UNIXFILE AT C_DOT INTO W_UNIXFIL1 W_UEXT.
  CLEAR W_UPATH.
  IF NOT W_UEXT IS INITIAL.
    CONCATENATE W_UNIXFIL1  C_DOT W_UEXT INTO W_UPATH.
  ELSE.
    W_UEXT = C_CSV.
    CONCATENATE W_UNIXFIL1 C_DOT W_UEXT INTO W_UPATH.
  ENDIF.


ENDFORM.                               " WRITE_TO_SERVER
[code]

[/code]

Former Member
0 Kudos
81
Hi, I have given the important routines you would need. You could you use this as reference.. you would have to build the rest . Please let me know if you are stuck ..

TABLES: CSKS.                          "Cost center master


************************************************************************
* Data Declaration
************************************************************************
DATA: BEGIN OF T_CSKS OCCURS 0,
         KOKRS LIKE CSKS-KOKRS,        "controlling area
         KOSTL LIKE CSKS-KOSTL,        "Cost center
         DATBI LIKE CSKS-DATBI,        "Valid to date
         DATAB LIKE CSKS-DATAB,        "Date valid from
         GSBER LIKE CSKS-GSBER,        "Business area
         BUKRS LIKE CSKS-BUKRS,        "Company code
         LOGSYSTEM LIKE CSKS-LOGSYSTEM,"Logical System
         MANDT LIKE SY-MANDT,          "client
         WAERS LIKE CSKS-WAERS,        "Currency
         PRCTR LIKE CSKS-PRCTR,        "Profit Centre
         VERAK LIKE CSKS-VERAK,        "Person in charge of cost Center
         RET_LOC(15),                  "Retail Location
      END OF T_CSKS.

*Download String data
DATA: BEGIN OF T_CSKS_X OCCURS 0,
         KOKRS(4), " like csks-kokrs,        "controlling area
         KOSTL(10), " like csks-kostl,        "Cost center
         LOGSYSTEM(3), " like csks-logsystem,"Logical System
          MANDT(3), " like sy-mandt,          "client
         DATBI(8), " like csks-datbi,        "Valid to date
         DATAB(8), " like csks-datab,        "Date valid from
         GSBER(4), " like csks-gsber,        "Business area
         BUKRS(4), " like csks-bukrs,        "Company code
         prctr(10), " like csks-prctr,        "Profit Centre
       waers(5), " like csks-waers,        "Currency
       verak(20), "like csks-verak,  "Person in charge of cost Center
        ret_loc(15),                  "Retail Location
      END OF T_CSKS_X.


************************************************************************
*  Start-of-Selection.                                                 *
************************************************************************
START-OF-SELECTION.
*collect data from csks
  PERFORM COLLECT_DATA.

*transfer to download tab.
  PERFORM DATA_TRANSFER.

*local
  IF P_PCFILE = C_YES.
    PERFORM LOCAL_DOWNLOAD.
  ENDIF.

*unix
  IF P_UNIX = C_YES.
    PERFORM UNIX_DOWNLOAD.
  ENDIF.

  IF P_PCFILE IS INITIAL AND P_UNIX IS INITIAL.
    MESSAGE  WITH 'Down load flags both are unchecked'.
  ENDIF.

END-OF-SELECTION.
 WRITE 😕 'PC File'  , C_UNDER, P_PCFIL.
* clear : p_pcfil, p_unfil.
*&---------------------------------------------------------------------*
*&      Form  COLLECT_DATA
*&---------------------------------------------------------------------*
*       Collect Data                                                   *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM COLLECT_DATA.
* Query from csks table.
  SELECT KOKRS        "Controlling area
         KOSTL        "Cost center
         DATBI        "Valid To Date
         DATAB        "Date Valid From
         GSBER        "Business area
         BUKRS        "Company code
         LOGSYSTEM    "Logical System
         WAERS        "Currency
         PRCTR        "Profit Centre
         INTO CORRESPONDING FIELDS OF
         TABLE T_CSKS
         FROM CSKS.
  W_PCFIL = P_PCFIL.
ENDFORM.                               " COLLECT_DATA

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD
*&---------------------------------------------------------------------*
*       Download                                                       *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DOWNLOAD.
  P_PCFIL =  W_PATH.
  CALL FUNCTION 'WS_DOWNLOAD'
       EXPORTING
           FILENAME            =  P_PCFIL
*           filetype            =  c_dat   "dat
           FILETYPE            =  'ASC' "c_dat   "dat
       TABLES
*            data_tab            = t_str
            DATA_TAB            = T_CSKS_X
*            fieldnames          = t_strhd
       EXCEPTIONS
            FILE_OPEN_ERROR     = 1
            FILE_WRITE_ERROR    = 2
            INVALID_FILESIZE    = 3
            INVALID_TABLE_WIDTH = 4
            INVALID_TYPE        = 5
            NO_BATCH            = 6
            UNKNOWN_ERROR       = 7
            OTHERS              = 8.
  IF SY-SUBRC EQ 0.
  ENDIF.

ENDFORM.                               " DOWNLOAD

*&---------------------------------------------------------------------*
*&      Form  WRITE_TO_SERVER
*&---------------------------------------------------------------------*
*      Write to server                                                 *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM WRITE_TO_SERVER.
  DATA : L_MSG(100) TYPE C,
         L_LINE(5000) TYPE C.
  P_UNFIL =  W_UPATH.

  OPEN DATASET P_UNFIL FOR OUTPUT IN TEXT MODE.        " message l_msg.

  IF SY-SUBRC <> 0.
    WRITE: / L_MSG.
  ENDIF.

*  loop at t_str.
  LOOP AT T_CSKS_X.
    TRANSFER T_CSKS_X TO P_UNFIL.
  ENDLOOP.
  CLOSE DATASET P_UNFIL.
  WRITE : / C_TEXT , W_UPATH.
  SPLIT W_UNIXFILE AT C_DOT INTO W_UNIXFIL1 W_UEXT.
  CLEAR W_UPATH.
  IF NOT W_UEXT IS INITIAL.
    CONCATENATE W_UNIXFIL1  C_DOT W_UEXT INTO W_UPATH.
  ELSE.
    W_UEXT = C_CSV.
    CONCATENATE W_UNIXFIL1 C_DOT W_UEXT INTO W_UPATH.
  ENDIF.


ENDFORM.                               " WRITE_TO_SERVER
[code]

[/code]