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

ABAP - Deleteing A Table In Archiving.

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


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


DATA: BEGIN OF I_SEL_FILES_ITAB OCCURS 0.
INCLUDE STRUCTURE ARCH_FILES.
DATA: END OF I_SEL_FILES_ITAB.

DATA: I_INDEX_TAB TYPE TABLE OF ARCH_IDX_S WITH HEADER LINE,

I_STAT_ITAB TYPE TABLE OF ARCH_STAT.

*- Structure........................................................
DATA: BEGIN OF K_BUFFER.
INCLUDE STRUCTURE arc_buffer.
DATA: END OF K_BUFFER.

DATA: K_TABLE_WA LIKE YAPTMAIN.

*Work Variables.....................................................
DATA: V_COMMIT_CNT LIKE ARCH_USR-ARCH_COMIT,
V_INDEX_FLAG LIKE ARCH_USR-ARCH_INDEX,
V_OBJECT_ID LIKE ARCH_IDX_S-OBJ_ID,
V_OFFSET LIKE ARCH_IDX_S-OBJ_OFFSET,
V_ARKEY LIKE ARCH_IDX_S-ARCHIVEKEY,
V_OBJECT_CNT TYPE I,
V_SUM_OBJECTS TYPE I VALUE 0,
V_SUM_OBJECTS1 TYPE I VALUE 0,
V_HANDLE LIKE SY-TABIX,
K_TABFIELD LIKE ARCH_STAT.

*Constants..........................................................
CONSTANTS: C_SPACE VALUE SPACE,
C_X VALUE 'X',
C_OBJECT(10) VALUE 'YARCHTEST',
C_TABLE(30) VALUE 'YAPTMAIN',
C_TEXT7(15) VALUE 'Test Run',
C_0 VALUE 0,
C_MSG1(70) VALUE 'No file can be accessed',
C_MSG2(70) VALUE 'Unable to access customizing data',
C_MSG3(70) VALUE 'Unable to provide statistics',
C_MSG4(70) VALUE 'Unable to Write statistics',
C_MSG5(70) VALUE 'Unable to process files for update',
C_MSG6(70) VALUE 'Unable to close archive session'.

*- Selection Screen...............................................
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE V_TEXT1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (29) V_TEST.
PARAMETERS: TESTRUN DEFAULT'X' AS CHECKBOX.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B1.


INITIALIZATION.
SY-TITLE = ' Delete Records for Object - YARCHTEST'.
V_TEXT1 = 'Selection Details'.
V_TEST = C_TEXT7.

START-OF-SELECTION.

PERFORM OPEN_ARCHIVE_FOR_DELETE.

PERFORM GET_ARCHIVE_CUST_DATA.

PERFORM DELETE_DATA.

PERFORM WRITE_STATISTICS.

PERFORM GET_ARCHIVE_FILES.

PERFORM CLOSE_ARCHIVE_FILE.

END-OF-SELECTION.

*& ---------------------------------------------------------*
*& Form OPEN_ARCHIVE_FOR_DELETE
*& -------------------------------------------------- *
* Open Archive File for Deleting Data
*& ---------------------------------------------------------*
FORM OPEN_ARCHIVE_FOR_DELETE.
CALL FUNCTION 'ARCHIVE_OPEN_FOR_DELETE'
EXPORTING
object = c_object
TEST_MODE = TESTRUN
IMPORTING
archive_handle = V_HANDLE
EXCEPTIONS
file_already_open = 1
FILE_IO_ERROR = 2
INTERNAL_ERROR = 3
NO_FILES_AVAILABLE = 4
OBJECT_NOT_FOUND = 5
OPEN_ERROR = 6
NOT_AUTHORIZED = 7
OTHERS = 8.
IF SY-SUBRC > C_0.
MESSAGE I001 WITH C_MSG1.
EXIT.
ENDIF.
ENDFORM.

*& ---------------------------------------------------------*
*& Form GET_ARCHIVE_CUST_DATA
*&----------------------------------------------------------*
* Get Customizing data for the Archive File
*& ---------------------------------------------------------*
FORM GET_ARCHIVE_CUST_DATA.
CALL FUNCTION 'ARCHIVE_GET_CUSTOMIZING_DATA'
EXPORTING
object = c_object
IMPORTING
COMMIT_COUNT_FOR_DELETE_PRG = V_COMMIT_CNT
EXCEPTIONS
OBJECT_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC > C_0.
MESSAGE I001 WITH C_MSG2.
EXIT.
ENDIF.
ENDFORM.

*& ---------------------------------------------------------*
*& Form DELETE_DATA
*&----------------------------------------------------------*
* Delete Archive Data
*& ---------------------------------------------------------*
FORM DELETE_DATA.

CLEAR: V_OBJECT_CNT.
CLEAR: V_SUM_OBJECTS,V_SUM_OBJECTS1.
DO.
ADD 1 TO V_OBJECT_CNT.
CALL FUNCTION 'ARCHIVE_GET_NEXT_OBJECT'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
IMPORTING
OBJECT_ID = V_OBJECT_ID
OBJECT_OFFSET = V_OFFSET
ARCHIVE_NAME = V_ARKEY
EXCEPTIONS
END_OF_FILE = 1
FILE_IO_ERROR = 2
INTERNAL_ERROR = 3
OPEN_ERROR = 4
WRONG_ACCESS_TO_ARCHIVE = 5
OTHERS = 6.
IF SY-SUBRC <> C_0.
EXIT.
ENDIF.

IF V_INDEX_FLAG IS INITIAL.
ELSEIF TESTRUN IS INITIAL.
MOVE: V_OBJECT_ID TO I_INDEX_TAB-OBJ_ID,
V_ARKEY TO I_INDEX_TAB-ARCHIVEKEY,
V_OFFSET TO I_INDEX_TAB-OBJ_OFFSET.
APPEND I_INDEX_TAB.
PERFORM SAVE_INDEX_TAB ON COMMIT.
ENDIF.

DO.
CALL FUNCTION 'ARCHIVE_GET_NEXT_RECORD'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
IMPORTING
RECORD = K_BUFFER-SEGMENT
RECORD_STRUCTURE = K_BUFFER-RNAME
EXCEPTIONS
END_OF_OBJECT = 1
INTERNAL_ERROR = 2
WRONG_ACCESS_TO_ARCHIVE = 3
OTHERS = 4.
IF SY-SUBRC <> C_0.
EXIT.
ENDIF.
CASE K_BUFFER-rname.
WHEN C_TABLE.
MOVE K_BUFFER-SEGMENT TO K_TABLE_WA.
APPEND K_TABLE_WA TO I_YAPTMAIN.
ENDCASE.
ENDDO.
IF TESTRUN IS INITIAL AND V_OBJECT_CNT = V_COMMIT_CNT.
DESCRIBE TABLE I_YAPTMAIN
LINES V_SUM_OBJECTS.
PERFORM DELETE_FROM_TABLE.
V_SUM_OBJECTS1 = V_SUM_OBJECTS1 + V_SUM_OBJECTS.
CLEAR: V_SUM_OBJECTS.
CLEAR I_YAPTMAIN.
REFRESH I_YAPTMAIN.
CLEAR V_OBJECT_CNT.
ENDIF.
ENDDO.

IF TESTRUN IS INITIAL AND V_OBJECT_CNT > 1.
PERFORM DELETE_FROM_TABLE.
ENDIF.
DESCRIBE TABLE I_YAPTMAIN
LINES V_SUM_OBJECTS.
REFRESH I_YAPTMAIN.
K_TABFIELD-TABNAME = C_SPACE.
V_SUM_OBJECTS1 = V_SUM_OBJECTS1 + V_SUM_OBJECTS.
K_TABFIELD-COUNT = V_SUM_OBJECTS1.
APPEND K_TABFIELD TO I_STAT_ITAB.
K_TABFIELD-TABNAME = C_TABLE.
K_TABFIELD-COUNT = V_SUM_OBJECTS1.
APPEND K_TABFIELD TO I_STAT_ITAB.
*& ---------------------------------------------------------*
CALL FUNCTION 'ARCHIVE_GIVE_STATISTICS'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
TABLES
TABLE = I_STAT_ITAB
EXCEPTIONS
INTERNAL_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG3.
EXIT.
ENDIF.
ENDFORM.
*& ---------------------------------------------------------*
*& Form WRITE_STATISTICS
*&----------------------------------------------------------*
* Write Statistics
*& ---------------------------------------------------------*
FORM WRITE_STATISTICS.
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_MSG4.
EXIT.
ENDIF.
ENDFORM.

*& ---------------------------------------------------------*
*& Form GET_ARCHIVE_FILES
*&----------------------------------------------------------*
* Get all processed files for update
*& ---------------------------------------------------------*
FORM GET_ARCHIVE_FILES.

CALL FUNCTION 'ARCHIVE_GET_ARCHIVE_FILES'
EXPORTING
ARCHIVE_HANDLE = V_HANDLE
TABLES
ARCHIVE_FILES = I_SEL_FILES_ITAB
EXCEPTIONS
WRONG_ACCESS_TO_ARCHIVE = 1
INTERNAL_ERROR = 2
OTHERS = 3.
IF SY-SUBRC <> C_0.
MESSAGE I001 WITH C_MSG5.
EXIT.
ENDIF.
ENDFORM.

*& ---------------------------------------------------------*
*& Form CLOSE_ARCHIVE_FILE
*&----------------------------------------------------------*
* Close Archive File
*& ---------------------------------------------------------*
FORM CLOSE_ARCHIVE_FILE.

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.
IF V_INDEX_FLAG IS INITIAL.

ELSEIF TESTRUN IS INITIAL.

CALL FUNCTION 'ARCHIVE_ADMIN_SET_STATUS'
EXPORTING
FILES_ARE_CONVERTED = C_SPACE
INDEX_CREATED = C_X
TABLES
ARCHIVE_FILES = I_SEL_FILES_ITAB
EXCEPTIONS
CANNOT_CHANGE_STATUS = 1
OTHERS = 2.
ENDIF.
ENDFORM.
*& ---------------------------------------------------------*
*& Form save_index_tab
*&----------------------------------------------------------*
FORM SAVE_INDEX_TAB.

DELETE ARCH_IDX_S FROM TABLE I_INDEX_TAB.
INSERT ARCH_IDX_S FROM TABLE I_INDEX_TAB.
REFRESH I_INDEX_TAB.

ENDFORM.
*& ---------------------------------------------------------*
*& Form DELETE_FROM_TABLE
*&----------------------------------------------------------*
* DELETE_FROM_TABLE
*& ---------------------------------------------------------*
FORM DELETE_FROM_TABLE.

DELETE YAPTMAIN FROM TABLE I_YAPTMAIN.
COMMIT WORK.
ENDFORM.

No comments:

Post a Comment