Download the Abap Internal Table to A File in SAP Application Server
Input.
Ta có bảng sau, chứa thông tin về một document
Ta có bảng sau, chứa thông tin về một document
Và có một form nhập thông tin đầu vào.
Một số Tcode
Output.
Đưa ra được file text có chứa data của bảng trên
Có 4 bước cơ bản để thực hiện việc download 1 bảng Internal table đến 1 file trong SAP Application Server
- Declare a Abap Internal table and Fill the internal table with required data.
- Use OPEN DATASET ABAP statement to open/create a file on the SAP Application Server
- Loop through the internal table and use TRANSFER TABLE statement to move each internal table record to file on application server.
- Close the file on the application server using CLOSE DATASET ABAP statement
Below program uses OPEN DATASET, TRANSFER AND CLOSE DATASET statement to download file
TABLES ZBKEG.
DATA: TT_ZBKEG TYPE STANDARD TABLE OF ZBKEG,
WA_ZBKEG TYPE ZBKEG.
INITIALIZATION.
SELECT-OPTIONS: S_BUKRS FOR ZBKEG-BUKRS,
S_GJAHR FOR ZBKEG-GJAHR,
S_BUDAT FOR ZBKEG-BUDAT,
S_WAERK FOR ZBKEG-WAERK.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF BLOCK DATA WITH FRAME TITLE TEXT-001.
PARAMETERS P_FILE TYPE FILETEXT-FILENAME OBLIGATORY.
SELECTION-SCREEN END OF BLOCK DATA.
START-OF-SELECTION.
PERFORM CHECK_PATH.
PERFORM GET_DATA.
PERFORM transfer.
PERFORM close.
END-of-SELECTION.
DESCRIBE TABLE tt_zbkeg.
"Sy-TFILL la bien dem line trong table
IF sy-tfill <> 0.
MESSAGE text-005 TYPE 'S' .
* WITH sy-tfill.
ELSEIF sy-tfill = 0.
MESSAGE text-006 TYPE 'S' DISPLAY LIKE 'E' .
ENDIF.
FORM CHECK_PATH.
DATA LV_DIR TYPE BTCH0000-TEXT80.
CALL FUNCTION 'TRINT_SPLIT_FILE_AND_PATH'
EXPORTING
FULL_NAME = P_FILE
IMPORTING
FILE_PATH = LV_DIR
EXCEPTIONS
X_ERROR = 1
OTHERS = 2
.
CALL FUNCTION 'PFL_CHECK_DIRECTORY'
EXPORTING
DIRECTORY = LV_DIR
EXCEPTIONS
PFL_DIR_NOT_EXIST = 1
PFL_PERMISSION_DENIED = 2
PFL_CANT_BUILD_DATASET_NAME = 3
PFL_FILE_NOT_EXIST = 4
PFL_AUTHORIZATION_MISSING = 5
OTHERS = 6
.
IF LV_DIR IS INITIAL.
MESSAGE TEXT-002 TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
OPEN DATASET P_FILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC <> 0.
MESSAGE TEXT-002 TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
FORM GET_DATA.
SELECT *
INTO TABLE TT_ZBKEG
FROM ZBKEG
WHERE BUKRS IN S_BUKRS " Company code
AND GJAHR IN S_GJAHR " Fiscal Year
AND BUDAT IN S_BUDAT " Posting Date
AND WAERK IN S_WAERK." Currency
ENDFORM.
FORM transfer.
LOOP AT tt_zbkeg INTO wa_zbkeg.
TRANSFER wa_zbkeg to p_file.
IF sy-subrc <> 0.
MESSAGE text-003 TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDLOOP.
ENDFORM.
FORM close.
close DATASET p_file.
IF sy-subrc <> 0.
MESSAGE text-004 TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDFORM.
DATA: TT_ZBKEG TYPE STANDARD TABLE OF ZBKEG,
WA_ZBKEG TYPE ZBKEG.
INITIALIZATION.
SELECT-OPTIONS: S_BUKRS FOR ZBKEG-BUKRS,
S_GJAHR FOR ZBKEG-GJAHR,
S_BUDAT FOR ZBKEG-BUDAT,
S_WAERK FOR ZBKEG-WAERK.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF BLOCK DATA WITH FRAME TITLE TEXT-001.
PARAMETERS P_FILE TYPE FILETEXT-FILENAME OBLIGATORY.
SELECTION-SCREEN END OF BLOCK DATA.
START-OF-SELECTION.
PERFORM CHECK_PATH.
PERFORM GET_DATA.
PERFORM transfer.
PERFORM close.
END-of-SELECTION.
DESCRIBE TABLE tt_zbkeg.
"Sy-TFILL la bien dem line trong table
IF sy-tfill <> 0.
MESSAGE text-005 TYPE 'S' .
* WITH sy-tfill.
ELSEIF sy-tfill = 0.
MESSAGE text-006 TYPE 'S' DISPLAY LIKE 'E' .
ENDIF.
FORM CHECK_PATH.
DATA LV_DIR TYPE BTCH0000-TEXT80.
CALL FUNCTION 'TRINT_SPLIT_FILE_AND_PATH'
EXPORTING
FULL_NAME = P_FILE
IMPORTING
FILE_PATH = LV_DIR
EXCEPTIONS
X_ERROR = 1
OTHERS = 2
.
CALL FUNCTION 'PFL_CHECK_DIRECTORY'
EXPORTING
DIRECTORY = LV_DIR
EXCEPTIONS
PFL_DIR_NOT_EXIST = 1
PFL_PERMISSION_DENIED = 2
PFL_CANT_BUILD_DATASET_NAME = 3
PFL_FILE_NOT_EXIST = 4
PFL_AUTHORIZATION_MISSING = 5
OTHERS = 6
.
IF LV_DIR IS INITIAL.
MESSAGE TEXT-002 TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
OPEN DATASET P_FILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC <> 0.
MESSAGE TEXT-002 TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
FORM GET_DATA.
SELECT *
INTO TABLE TT_ZBKEG
FROM ZBKEG
WHERE BUKRS IN S_BUKRS " Company code
AND GJAHR IN S_GJAHR " Fiscal Year
AND BUDAT IN S_BUDAT " Posting Date
AND WAERK IN S_WAERK." Currency
ENDFORM.
FORM transfer.
LOOP AT tt_zbkeg INTO wa_zbkeg.
TRANSFER wa_zbkeg to p_file.
IF sy-subrc <> 0.
MESSAGE text-003 TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDLOOP.
ENDFORM.
FORM close.
close DATASET p_file.
IF sy-subrc <> 0.
MESSAGE text-004 TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDFORM.
Comments
Post a Comment