Skip to Content

Hi,

I attach a utility to copy any contents of a dictionary table to another in a dynamic way (common fields are inserted into destination table). You have the option to clean the target table and make a backup of it before.

CODE:

*&———————————————————————*
*& REPORT  ZCOPY_TABLE
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

REPORT ZCOPY_TABLE.

“CREATING A PIVOTTABLE IS:
“DEFINE THE FIELD SYMBOL THAT WILL CONTAIN THE INTERNAL TABLE
FIELD-SYMBOLS: <WA_TABLE_O>.
FIELD-SYMBOLS: <TBL_O> TYPE STANDARD TABLE.
FIELD-SYMBOLS: <WA_TABLE_D>.
FIELD-SYMBOLS: <TBL_D> TYPE STANDARD TABLE.

“DEFINES THE TYPE POINTER THAT WILL POINT TO THE INTERNAL TABLE

DATA: T_TABLE_O TYPE REF TO DATA.
DATA: WA_TABLE_O TYPE REF TO DATA.
DATA: G_NOMBRE_TABLE_O TYPE TABNAME16.

DATA: T_TABLE_D TYPE REF TO DATA.
DATA: WA_TABLE_D TYPE REF TO DATA.
DATA: G_NOMBRE_TABLE_D TYPE TABNAME16.

DATA: MSG TYPE STRING OCCURS 0 WITH HEADER LINE.
DATA: ANSWER TYPE STRING.
DATA: DBFIELDS TYPE STANDARD TABLE OF DBFIELD WITH HEADER LINE.
DATA: BEGIN OF DBFIELDS_AUX OCCURS 0.
DATA: NAME TYPE DBFIELDNAME.
DATA: END OF  DBFIELDS_AUX.
DATA: DEF_PATH(50) TYPE C VALUE ‘C:\’.

TYPE-POOLS: TRUXS.

SELECTION-SCREEN BEGIN OF BLOCK BL1 WITH FRAME TITLE TEXT001.
   PARAMETERS: G_TABL_O TYPE TABNAME16.
   PARAMETERS: G_TABL_D TYPE TABNAME16.
   SELECTION-SCREEN SKIP 1.
   PARAMETERS: BACKUP AS CHECKBOX DEFAULT ‘X’.
   PARAMETERS: P_FILE(128) TYPE C LOWER CASE.
   SELECTION-SCREEN SKIP 1.
   PARAMETERS: DEL_TABL AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK BL1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  CALL FUNCTION ‘WS_FILENAME_GET’
  EXPORTING
  DEF_FILENAME           = P_FILE
  DEF_PATH               = DEF_PATH
  MASK                   = ‘,*.*,*.*.’
  MODE                   = ‘O’
  TITLE                  = ‘SELECT PATH & FILENAME: ‘
  IMPORTING
  FILENAME               = P_FILE
  EXCEPTIONS
  INV_WINSYS             = 1
  NO_BATCH               = 2
  SELECTION_CANCEL       = 3
  SELECTION_ERROR        = 4
  OTHERS                 = 5
  .
  IF SYSUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

START-OF-SELECTION.

IF BACKUP IS NOT INITIAL AND P_FILE IS INITIAL.
   MESSAGE ‘PLEASE FILL THE FILE PATH’ TYPE  ‘I’.
   EXIT.
ENDIF.

IF G_TABL_D(1) NE ‘Z’.
   MESSAGE ‘ONLY TABLES Z IN TABLE DESTINATION’ TYPE ‘I’.
   EXIT.
ENDIF.

CREATE DATA T_TABLE_O TYPE STANDARD TABLE OF (G_TABL_O).
ASSIGN T_TABLE_O->* TO <TBL_O>.

CREATE DATA  WA_TABLE_O LIKE LINE OF <TBL_O>.
ASSIGN WA_TABLE_O->* TO <WA_TABLE_O>.

CREATE DATA T_TABLE_D TYPE STANDARD TABLE OF (G_TABL_D).
ASSIGN T_TABLE_D->* TO <TBL_D>.

CREATE DATA  WA_TABLE_D LIKE LINE OF <TBL_D>.
ASSIGN WA_TABLE_D->* TO <WA_TABLE_D>.

IF BACKUP IS NOT INITIAL.
   PERFORM BACKUP_TABLE.
ENDIF.

IF DEL_TABL IS NOT INITIAL.
    CONCATENATE ‘CAN YOU DELETE CONTENT OF TABLE’ G_TABL_D ‘?’ INTO MSG SEPARATED BY SPACE.
    CALL FUNCTION ‘POPUP_TO_CONFIRM’
      EXPORTING
*      TITLEBAR                    = ‘ ‘
*      DIAGNOSE_OBJECT             = ‘ ‘
        TEXT_QUESTION               = MSG
*      TEXT_BUTTON_1               = ‘JA'(001)
*      ICON_BUTTON_1               = ‘ ‘
*      TEXT_BUTTON_2               = ‘NEIN'(002)
*      ICON_BUTTON_2               = ‘ ‘
*      DEFAULT_BUTTON              = ‘1’
*      DISPLAY_CANCEL_BUTTON       = ‘X’
*      USERDEFINED_F1_HELP         = ‘ ‘
*      START_COLUMN                = 25
*      START_ROW                   =6
*      POPUP_TYPE                  =
*      IV_QUICKINFO_BUTTON_1       = ‘ ‘
*      IV_QUICKINFO_BUTTON_2       = ‘ ‘
     IMPORTING
       ANSWER                      = ANSWER
*    TABLES
*      PARAMETER                   =
*    EXCEPTIONS
*      TEXT_NOT_FOUND              = 1
*      OTHERS                      = 2
              .
    CLEAR MSG.
    IF ANSWER NE ‘1’.
      MESSAGE ‘COPY HAS BEEN CANCELLED’ TYPE ‘I’.
      EXIT.
    ELSE.
      DELETE FROM (G_TABL_D).
      COMMIT WORK.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
ENDIF.

SELECT * FROM (G_TABL_O) INTO CORRESPONDING FIELDS OF TABLE <TBL_O>.
IF SYSUBRC = 0.
   LOOP AT <TBL_O> INTO <WA_TABLE_O>. “ITERAMOS SOBRE LA TABLE ORIGEN
    MOVE-CORRESPONDING <WA_TABLE_O> TO <WA_TABLE_D>. “SE PASAN LOS CAMPOS AL WORK-AREA DE DESTINO(EN MI CASO SE PASABAN SÓLO CIERTOS CAMPO)
    INSERT INTO (G_TABL_D) VALUES <WA_TABLE_D>.“SE INSERTA EN LA TABLE DE DICCIONARIO
    IF SYSUBRC NE 0.
      CLEAR MSG.
      MOVE SYTABIX TO MSG.
      CONCATENATE ‘ERROR WHEN INSERT THE REGISTRY LINE:’ MSG INTO MSG.
      APPEND MSG.
    ENDIF.
   ENDLOOP.
   LOOP AT MSG.
   WRITE: MSG.
   SKIP.
   ENDLOOP.
   IF MSG IS INITIAL.
     WRITE: ‘ALL RECORDS ARE COPIED’.
   ENDIF.
ENDIF.

FORM BACKUP_TABLE.

SELECT * FROM (G_TABL_D) INTO CORRESPONDING FIELDS OF TABLE <TBL_D>.
IF SYSUBRC NE 0.
   EXIT.
ENDIF.

MOVE G_TABL_D TO DBFIELDS_AUXNAME.

CALL FUNCTION ‘DB_GET_TABLE_FIELDS’
EXPORTING
   TABNAME = DBFIELDS_AUXNAME
TABLES
   DBFIELDS = DBFIELDS.
IF SYSUBRC = 0.
   LOOP AT DBFIELDS.
     MOVE DBFIELDSNAME TO DBFIELDS_AUX.
     APPEND DBFIELDS_AUX.
   ENDLOOP.
ELSE.
   EXIT.
ENDIF.

IF <TBL_D> IS ASSIGNED.

  CALL FUNCTION ‘WS_DOWNLOAD’
  EXPORTING
   FILENAME                      = P_FILE
   FILETYPE                      = ‘DAT’ ” DAT OR ASCI
  TABLES
   DATA_TAB                      = <TBL_D>
   FIELDNAMES                    = DBFIELDS_AUX
  EXCEPTIONS
   FILE_OPEN_ERROR               = 1
   FILE_WRITE_ERROR              = 2
   INVALID_FILESIZE              = 3
   INVALID_TYPE                  = 4
   NO_BATCH                      = 5
   UNKNOWN_ERROR                 = 6
   INVALID_TABLE_WIDTH           = 7
   GUI_REFUSE_FILETRANSFER       = 8
   CUSTOMER_ERROR                = 9
   NO_AUTHORITY                  = 10
   OTHERS                        = 11
  .
  IF SYSUBRC <> 0.
    MESSAGE ID SYMSGID TYPE SYMSGTY NUMBER SYMSGNO
    WITH SYMSGV1 SYMSGV2 SYMSGV3 SYMSGV4.
  ELSE.
    MESSAGE ‘FILE WAS SAVED’ TYPE ‘I’.
  ENDIF.
  ELSE.
    MESSAGE ‘NO DATA’ TYPE ‘I’.
  ENDIF.
ENDFORM.

*****************

Best regards.

Julio.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply