Skip to Content

Continuing with my new

Taking good care of

weblog series Taking good care of Z tables, I wanted to show you a simple code to make backups of you Z Tables data. I’m using ABAP Objects for this one, because it easier and looks better.

I know that this report could be easy for the most of us, but for the newbies it could be helpful.

Again, the interface is very simple:

image

The source code is easy, especially if you have some ABAP OO background:

ZTABLE_ELEMENTS

REPORT Z_DUMMY_ATG NO STANDARD PAGE HEADING MESSAGE-ID SAPLWOSA.

*=======================================================================

  • Variables

*=======================================================================

DATA DESCR_STRUCT_REF TYPE REF TO CL_ABAP_STRUCTDESCR.

DATA WA_FCAT TYPE LVC_S_FCAT.

DATA IT_FIELDCATALOG TYPE LVC_T_FCAT.

DATA DATAREF TYPE REF TO DATA.

DATA: ONE  LIKE PCFILE-DRIVE,

      TWO  LIKE PCFILE-PATH,

      LONG TYPE I,

      FLAG TYPE C,

      FILEPATH(128) TYPE C,

      FILE_TAB TYPE STRING,

      TABNAME LIKE DD02L-TABNAME.

*=======================================================================

  • Field-Symbols.

*=======================================================================

FIELD-SYMBOLS:

                TYPE ANY.

*=======================================================================

  • Selection screen

*=======================================================================

SELECTION-SCREEN BEGIN OF BLOCK DATA WITH FRAME TITLE TEXT-T01.

PARAMETERS:

    TABNAM(128) TYPE C,

    FUNCTION(1) TYPE C OBLIGATORY,

    LISTNAME LIKE RLGRAP-FILENAME.

SELECTION-SCREEN END OF BLOCK DATA.

*=======================================================================

  • At Selection screen

*=======================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR LISTNAME.

  PERFORM GET_FILENAME CHANGING LISTNAME.

*=======================================================================

  • Start-of-selection

*=======================================================================

START-OF-SELECTION.

  CLEAR FLAG.

  PERFORM LOAD_DATA USING TABNAM.

  PERFORM VERIFY_TABLE USING TABNAM CHANGING FLAG.

  IF FLAG NE ‘X’.

    IF FUNCTION EQ ‘D’.

      PERFORM CREATE_TABLE USING TABNAM.

      PERFORM DOWNLOAD_TABLE USING TABNAM.

    ELSE.

      PERFORM CREATE_TABLE USING TABNAM.

      PERFORM UPLOAD_TABLE USING TABNAM.

    ENDIF.

  ELSE.

    MESSAGE S000 WITH ‘The proposed table doesn”t exist.’.

  ENDIF.

—-


  •       FORM GET_FILENAME                                             *

—-


  •       Name of the directory.                                    

—-


FORM GET_FILENAME CHANGING LISTNAME.

  CALL FUNCTION ‘WS_FILENAME_GET’

       EXPORTING

            DEF_FILENAME     = LISTNAME

            DEF_PATH         = ‘C:downloadslist’

            MASK             = ‘,.,.. ‘

            MODE             = ‘S’

            TITLE            = ‘Save as’

       IMPORTING

            FILENAME         = LISTNAME

       EXCEPTIONS

            INV_WINSYS       = 1

            NO_BATCH         = 2

            SELECTION_CANCEL = 3

            SELECTION_ERROR  = 4

            OTHERS           = 5.

ENDFORM.

—-


  •       FORM LOAD_DATA                                                *

—-


  •       Specifies the path and table name

—-


FORM LOAD_DATA USING MY_TAB.

  DATA: W_FILE LIKE PCFILE-PATH.

  W_FILE = LISTNAME.

  CALL FUNCTION ‘PC_SPLIT_COMPLETE_FILENAME’

       EXPORTING

            COMPLETE_FILENAME = W_FILE

       IMPORTING

            DRIVE             = ONE

            PATH              = TWO

       EXCEPTIONS

            INVALID_DRIVE     = 1

            INVALID_EXTENSION = 2

            INVALID_NAME      = 3

            INVALID_PATH      = 4

            OTHERS            = 5.

  CONCATENATE ONE ‘:’ TWO INTO FILEPATH.

  CONCATENATE FILEPATH MY_TAB ‘.txt’ INTO FILE_TAB.

ENDFORM.

—-


  •       FORM DOWNLOAD_TABLE                                           *

—-


  •       Downloads the table data.

—-


FORM DOWNLOAD_TABLE USING MY_TAB.

  SELECT *

  INTO TABLE

       EXCEPTIONS

            FILE_WRITE_ERROR        = 1

            NO_BATCH                = 2

            GUI_REFUSE_FILETRANSFER = 3

            INVALID_TYPE            = 4

            NO_AUTHORITY            = 5

            UNKNOWN_ERROR           = 6.

  IF SY-SUBRC EQ 0.

    CALL FUNCTION ‘POPUP_TO_INFORM’

         EXPORTING

              TITEL = ‘Successful Download’

              TXT1  = ‘All the data from the table’

              TXT2  = ‘was correctly downloaded.’.

  ELSE.

    CALL FUNCTION ‘POPUP_TO_INFORM’

         EXPORTING

              TITEL = ‘Download Error!’

              TXT1  = ‘The data of the table’

              TXT2  = ‘couldn”t be downloaded.’.

  ENDIF.

ENDFORM.

—-


  •       FORM UPLOAD_TABLE                                           *

—-


  •       Table Upload.

—-


FORM UPLOAD_TABLE USING MY_TAB.

  ASSIGN .

  IF SY-SUBRC EQ 0.

    CALL FUNCTION ‘POPUP_TO_INFORM’

         EXPORTING

              TITEL = ‘Successful Upload’

              TXT1  = ‘All the data from the table’

              TXT2  = ‘was correctly uploaded.’.

  ELSE.

    CALL FUNCTION ‘POPUP_TO_INFORM’

         EXPORTING

              TITEL = ‘Upload Error!’

              TXT1  = ‘The data of the table’

              TXT2  = ‘couldn”t be uploaded.’.

  ENDIF.

ENDFORM.

—-


  •       FORM CREATE_TABLE                                             *

—-


  •       Creates a dynamic internal table.

—-


FORM CREATE_TABLE USING MY_TAB.

  CREATE DATA DATAREF TYPE (MY_TAB).

  ASSIGN DATAREF->* TO .

ENDFORM.

&—-


*&      Form  VERIFY_TABLE

&—-


  •       The table must exist!

—-


FORM VERIFY_TABLE USING TABNAM CHANGING FLAG.

  SELECT SINGLE TABNAME

  INTO (TABNAME)

  FROM DD02L

  WHERE TABNAME EQ TABNAM.

  IF SY-SUBRC NE 0.

    FLAG = ‘X’.

  ENDIF.

ENDFORM.

Hope you like this one…I got some more under my belt…Just wait for them -:)

Note: After publishing it was brought to my attention the existence of another similiar program, as the saying goes “Great minds think alike” and it seems that I was not the only one to find this code useful!

http://www.sapfans.com/sapfans/repos/neil.htm

To report this post you need to login first.

11 Comments

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

  1. Bruno Mikio Barros
    Mr. Tejada has post a good tool for ABAP programmers again!

    Unfortunately, it does not work properly with tables with currency fields (I am not sure, just guessing).

    (0) 
    1. Alvaro Tejada Galindo Post author
      Hi Bruno:

      Thanx for you kind words -:)

      I have tested my code with table BKPF and it worked just fine -;)  Of course…I maded a Z version of it…Download the data from the original BKPF and uploaded to my Z_BKPF and it was ok. -;)

      Anyway, it you found any error on it, please let me know so i can fixed it.

      And stay tuned, because i have some more tools to release in a couple of days.

      Greetings,

      Blag.

      (0) 
      1. Bruno Mikio Barros
        Hello,

        I have found the problem: I have tested the program in an 4.6 environment, that causes error in FM GUI_UPLOAD.

        OK! not your fault!

        Good work man 🙂

        Regards.

        Bruno

        (0) 
        1. Craig Cmehil
          It seems the programs may do the same thing but the are quite different in terms of methods of working.

          Granted it’s a close one to call, but Alvaro has assured me that he was not aware of this other program before hand. However I hav asked him to update the blog and make a mention of this other program now that it has come to light.

          Craig

          (0) 
          1. Alvaro Tejada Galindo Post author
            That’s right…I didn’t know that the tool already exists…Besides, we’re using different programming techniques. As you can see, i have add a short message explanation incluiding the SapFans link.

            Greetings,

            Blag.

            (0) 
  2. Sharad Agrawal
    Hi Alvaro,
    This weblog is misnomer because this solution is oversimplification of what you do as table maintenance. This following functionality is missing from this tool and needs to be enhanced to make it useful.

    (1) There is no validation for data type being loaded in table field. If you try to load nonnumeric data into numeric field, it will dump out.

    (2) There is no provision of filtering down of data in downloading.

    (3)This toold does not check for foreign lock before modifying it. The table entries should be locked before making any change.

    (4)No check for referential integrity.

    All these features can be added in the existing program in generic way.

    (0) 
    1. Alvaro Tejada Galindo Post author
      Hi Sharad:

      What you are saying it true…but remember, this a tool for newbies…also, it’s supposed to be used to transfer Data from a SAP System into a MiniSap System, or to make backups of Z Tables Data.

      I don’t think that you need data validation, because you’re supposed to upload the same data you have download…

      If you want to improve the tool, go ahead…I got no problem with it.

      Greetings,

      (0) 

Leave a Reply