Google Search - Blog...........

ABAP - Program For Archiving A Table

REPORT YARCHTEST_ARC MESSAGE-ID YAPK
NO STANDARD PAGE HEADING
LINE-SIZE 255.

*- Tables Declarations.
TABLES: YAPTMAIN.

*- Internal Tables Declarations.
DATA: BEGIN OF I_YAPTMAIN OCCURS 0.
INCLUDE STRUCTURE YAPTMAIN .
DATA: END OF I_YAPTMAIN.

*- Data Type Declarations.
DATA:
V_BUTTON(1) TYPE C,
V_HANDLE LIKE SY-TABIX,
V_CREATE(1) TYPE C VALUE 'X',
V_NO_DELETE(1) TYPE C VALUE 'X',
V_DATA_OBJECT_ID LIKE ARCH_IDX_S-OBJ_ID,
V_COUNT(10) TYPE N.

*- Constants.
CONSTANTS:
C_X VALUE 'X',
C_SPACE VALUE '',
C_NOFILE(30) VALUE 'Donot Create Archive File',
C_DTEST(30) VALUE 'Delete Job in Test Mode',
C_DPROD(30) VALUE 'Delete Program',
C_OBJECT(10) VALUE 'YARCHTEST',
C_TABLE(30) VALUE 'YAPTMAIN',
C_0 VALUE 0,
C_TEXT1(70) VALUE 'Created by ArchivePac',
C_MSG1(70) VALUE 'Unable to open archive session for Archive object',
C_MSG2(70) VALUE 'Unable to Access Archive object',
C_MSG3(70) VALUE 'Invalid record structure',
C_MSG4(70) VALUE 'Unable to write data to archive file',
C_MSG5(70) VALUE 'Unable to create spool list',
C_MSG6(70) VALUE 'Unable to close archive session'.

*- Selection Screen.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE V_TEXT1.
SELECT-OPTIONS: S_ARCH01 FOR YAPTMAIN-ARCHDATE.
SELECT-OPTIONS: S_ARCH02 FOR YAPTMAIN-ARCH_COMP.
SELECT-OPTIONS: S_ARCH03 FOR YAPTMAIN-ARCH_FREQ.
SELECTION-SCREEN END OF BLOCK B1.

SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE V_TEXT2.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (29) V_NOFILE.
PARAMETERS: NO_FILE LIKE V_BUTTON RADIOBUTTON GROUP RADI.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (29) V_DTEST.
PARAMETERS: DEL_TEST LIKE V_BUTTON RADIOBUTTON GROUP RADI.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (29) V_DPROD.
PARAMETERS: DEL_PROD LIKE V_BUTTON RADIOBUTTON GROUP RADI.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B2.

INITIALIZATION.
SY-TITLE = ' Archive Program for Table - YAPTMAIN'.
V_TEXT1 = 'Selection Details'.
*- Get Selection Screen Texts.
PERFORM GET_SELECTION_TEXTS.

START-OF-SELECTION.

PERFORM BUILD_ADK_PARAMETERS.

PERFORM GET_ARCHIVE_DATA.

PERFORM OPEN_ARCHIVE.

PERFORM WRITE_ARCHIVE_RECORDS TABLES I_YAPTMAIN
USING C_TABLE.

END-OF-SELECTION.

PERFORM WRITE_STATISTICS.

PERFORM CLOSE_ARCHIVE_FILE.


*& ---------------------------------------------------------*
*& Form BUILD_ADK_PARAMETERS
*&---------------------------------------------------------*
* Build parameters for Archiving Object
*&-----------------------------------------------------------*
FORM BUILD_ADK_PARAMETERS.
* build ADK parameters from chosen button
IF NO_FILE = C_X.
V_CREATE = C_SPACE.
V_NO_DELETE = C_SPACE.
ENDIF.
IF DEL_TEST = C_X.
V_CREATE = C_X.
V_NO_DELETE = C_X.
ENDIF.
IF DEL_PROD = C_X.
V_CREATE = C_X.
V_NO_DELETE = C_SPACE.
ENDIF.

ENDFORM.
*& ---------------------------------------------------------*
*& Form GET_ARCHIVE_DATA
*& ---------------------------------------------------------*
* Get relevant archive data from the database
*& ---------------------------------------------------------*
FORM GET_ARCHIVE_DATA.
* Select data to be archived
SELECT * FROM YAPTMAIN INTO TABLE I_YAPTMAIN WHERE
ARCHDATE IN S_ARCH01 AND
ARCH_COMP IN S_ARCH02 AND
ARCH_FREQ IN S_ARCH03.
ENDFORM.

*& ---------------------------------------------------------*
*& Form OPEN_ARCHIVE
*& ---------------------------------------------------------*
* Open Archive for writing data
*& ---------------------------------------------------------*
FORM OPEN_ARCHIVE.
* open a new archive session to archive data
CALL FUNCTION 'ARCHIVE_OPEN_FOR_WRITE'
EXPORTING
CALL_DELETE_JOB_IN_TEST_MODE = V_NO_DELETE
CREATE_ARCHIVE_FILE = V_CREATE
OBJECT = C_OBJECT
COMMENTS = C_TEXT1
IMPORTING
ARCHIVE_HANDLE = V_HANDLE
EXCEPTIONS
INTERNAL_ERROR = 1
OBJECT_NOT_FOUND = 2
OPEN_ERROR = 3
NOT_AUTHORIZED = 4
OTHERS = 5.
IF SY-SUBRC <> C_0.
MESSAGE E001 WITH C_MSG1.
ENDIF.
ENDFORM.

*& ---------------------------------------------------------*
*& Form WRITE_ARCHIVE_RECORDS
*& ---------------------------------------------------------*
* Write Archive records to archive file
*& ---------------------------------------------------------*
FORM WRITE_ARCHIVE_RECORDS TABLES P_TABLE
USING VALUE(P_STRUC).
* close the archive session
SORT P_TABLE.
LOOP AT P_TABLE.
V_COUNT = V_COUNT + 1.
CONCATENATE SY-MANDT V_COUNT INTO V_DATA_OBJECT_ID.

* initialize a new data object
CALL FUNCTION 'ARCHIVE_NEW_OBJECT'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
OBJECT_ID = V_DATA_OBJECT_ID
EXCEPTIONS
INTERNAL_ERROR = 1
WRONG_ACCESS_TO_ARCHIVE = 2
OTHERS = 3.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG2.
EXIT.
ENDIF.

* put data records into ADKS data container
CALL FUNCTION 'ARCHIVE_PUT_RECORD'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
RECORD = P_TABLE
RECORD_STRUCTURE = P_STRUC
EXCEPTIONS
INTERNAL_ERROR = 1
WRONG_ACCESS_TO_ARCHIVE = 2
INVALID_RECORD_STRUCTURE = 3
OTHERS = 4.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG3.
EXIT.
ENDIF.

* write data object into the archive file
CALL FUNCTION 'ARCHIVE_SAVE_OBJECT'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
EXCEPTIONS
FILE_IO_ERROR = 1
INTERNAL_ERROR = 2
OPEN_ERROR = 3
TERMINATION_REQUESTED = 4
WRONG_ACCESS_TO_ARCHIVE = 5
OTHERS = 6.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG4.
EXIT.
ENDIF.

ENDLOOP.
ENDFORM.

*& ---------------------------------------------------------*
*& Form WRITE_STATISTICS
*& ---------------------------------------------------------*
* Write Archive File statistics
*& ---------------------------------------------------------*
FORM WRITE_STATISTICS.
* create the default spool list

CALL FUNCTION 'ARCHIVE_WRITE_STATISTICS'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
EXCEPTIONS
INTERNAL_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG5.
EXIT.
ENDIF.

ENDFORM.

*& ---------------------------------------------------------*
*& Form CLOSE_ARCHIVE_FILE
*& -------------------------------------------------- *
* Close Archive File
*& ---------------------------------------------------------*
FORM CLOSE_ARCHIVE_FILE.
* close the archive session

CALL FUNCTION 'ARCHIVE_CLOSE_FILE'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
EXCEPTIONS
INTERNAL_ERROR = 1
WRONG_ACCESS_TO_ARCHIVE = 2
OTHERS = 3.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG6.
EXIT.
ENDIF.

ENDFORM.

FORM GET_SELECTION_TEXTS.
V_NOFILE = C_NOFILE.
V_DTEST = C_DTEST.
V_DPROD = C_DPROD.
ENDFORM.

No comments:

Post a Comment