Export SAP tables to tab-delimited UTF-8 text file with header row

Jimbo's picture

It never hurts to take a snapshot of high-profile tables in the production system before making changes. This task can be a manual task that is performed each day or broken down into object-specific tasks that automatically export pertinent tables as part of the Convert Data step.

https://grist.files.wordpress.com/2013/08/mountain-goat-eating-flickr.png?w=660&h=489|https://s-media-cache-ak0.pinimg.com/600x315/e4/c0/a6/e4c0a6b3944ab01dfadc26445b9db56a.jpg|http://cdn.thedailybeast.com/content/dailybeast/galleries/2012/02/16/goats-have-accents-and-other-strange-goat-facts-photos/jcr:content/gallery/slide_5/image.img.1280.1024.jpg/1337256000000.cached.jpg|http://media.mnn.com/assets/images/2015/03/GoatsInTress01-m-0302.jpg.653x0_q80_crop-smart.jpgExporting from SE16 leaves a lot to be desired. It is time-consuming and the file format is cumbersome to work with. This method exports the data in the most commonly used format: tab-delimited text. With header row!

There is a lot of content on the internet that deals with the GUI_DOWNLOAD function. This function by itself is very powerful, but lacks the functionality to add a header row at the top of the text file. The header row makes working with the data in MS Access, MS Excel or LSMW much easier because every column is already identified. The snippet below adds a header row to the text file with the SAP field names.

Add these two forms to a User Defined Routine or to the FORM_ROUTINES section of the LSMW Change Field Mapping and Conversion Rules. This is where most of the work gets done.

form ExportTable using lvTable lvFilename.
* Note: This code performs no error checking.  It is assumed that
* the table name and the file name are both valid.
  data: lvDD03L like DD03L, lvMessage type string,
   tab type c value cl_abap_char_utilities=>horizontal_tab.

* Let the user know what is happening to reduce boredom.
  concatenate 'Exporting' lvTable '. . .' into lvMessage
   separated by space.
  call function 'SAPGUI_PROGRESS_INDICATOR'
    exporting
       TEXT = lvMessage
    exceptions
      others = 1.

*
* First we make the header for the text file.  It's simply
* concatenating fieldnames from the DD03L table.
*
  types: begin of t_Header,
          header(3000) type c,
         end of t_Header.
  data: it_Header type standard table of t_Header initial size 0,
        wa_Header type t_Header.
  select * from DD03L into lvDD03L
   where TABNAME eq lvTable and ROLLNAME ne '' order by POSITION.
    if wa_Header-header eq ''.
      wa_Header-header = lvDD03L-FIELDNAME.
    else.
      concatenate wa_Header-header lvDD03L-FIELDNAME
       into wa_Header-header separated by tab.
    endif.
  endselect.
  append wa_Header to it_Header.
*
* Now we have an internal table with one record holding a
* tab-delimited string with all the field names.  We write
* this as the beginning of the file.
*
  perform ExportTableWrite using it_Header lvFilename ''.

*
* Now we create a flexible internal table with the contents of the
* table to be exported.
*
  data: w_tabname type w_tabname,
   w_dref type ref to data.
  FIELD-SYMBOLS: <t_itab> TYPE standard TABLE, <wa_itab>.
  w_tabname = lvTable. translate w_tabname to UPPER CASE.
  CREATE DATA w_dref TYPE standard TABLE OF (w_tabname).
  ASSIGN w_dref->* TO <t_itab>.

* Load the contents of the table to be exported into an internal table.
  select * from (w_tabname) into table <t_itab>.

* Now we append the contents of the internal table to the text
* file that already has the header row.
  perform ExportTableWrite using <t_itab> lvFilename 'X'.
endform.

Form ExportTableWrite using lvInternalTable type standard table
 lv_Filename lv_Append.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      FILENAME                        = lv_Filename
      FILETYPE                        = 'ASC'
      APPEND                          = lv_Append
      CODEPAGE                        = '4310'  "UTF-8
      WRITE_FIELD_SEPARATOR = 'X'
      HEADER = '00'
      TRUNC_TRAILING_BLANKS = 'X'
    TABLES
      DATA_TAB                        = lvInternalTable
    EXCEPTIONS
      FILE_WRITE_ERROR                = 1
      OTHERS                          = 2.
endform.

Now add a line for each table to be exported. The parameters are the name of the table and the file name on the local hard drive.

perform ExportTable using 'MARA' 'c:\temp\SAP_MARA.txt'.
perform ExportTable using 'MARC' 'c:\temp\SAP_MARC.txt'.
perform ExportTable using 'MARD' 'c:\temp\SAP_MARD.txt'.
perform ExportTable using 'MARM' 'c:\temp\SAP_MARM.txt'.
perform ExportTable using 'MEAN' 'c:\temp\SAP_MEAN.txt'.
perform ExportTable using 'MBEW' 'c:\temp\SAP_MBEW.txt'.
perform ExportTable using 'MAKT' 'c:\temp\SAP_MAKT.txt'.
perform ExportTable using 'LFA1' 'c:\temp\SAP_LFA1.txt'.
perform ExportTable using 'LFB1' 'c:\temp\SAP_LFB1.txt'.
perform ExportTable using 'LFBK' 'c:\temp\SAP_LFBK.txt'.
perform ExportTable using 'KNA1' 'c:\temp\SAP_KNA1.txt'.
perform ExportTable using 'KNB1' 'c:\temp\SAP_KNB1.txt'.
perform ExportTable using 'KNBK' 'c:\temp\SAP_KNBK.txt'.
perform ExportTable using 'KNVV' 'c:\temp\SAP_KNVV.txt'.
perform ExportTable using 'KNVP' 'c:\temp\SAP_KNVP.txt'.

It's possible to use a query to pull table names and build a file name based on the table name. Take care as the process may take a very long time when many tables are selected. When building the file name, be sure to store it in a string variable.

 
data: lDD02L like DD02L, cFileName type string.
select * from DD02L into lDD02L
where ( TABNAME like 'MA%' or TABNAME like 'LF%' or TABNAME like 'KN%' )
   and TABCLASS eq 'TRANSP'.
  concatenate 'c:\temp\SAP_' lDD02L-TABNAME '.txt' into cFilename.
  perform ExportTable using lDD02L-TABNAME cFilename.
endselect.

There is no way that this snippet could have been created without a little help from outside the SAPLSMW community. We take a moment here to link to those sites that contributed unwittingly.

  • The know-how to create a dynamic internal table came from saptechnical.com. Very special thanks goes out to Vijayanand Poreddy for a great article including the source code that can be used to create and populate an internal table starting with no more than the SAP table name.
  • There are countless articles on the internet about handling the tab character in ABAP, but this was near the top of the Google search and it was a well written article. Special thanks to the unnamed author of this article.
  • Thanks to QuiGou Wu from this page for explaining how to coax SAP into producing data in UTF-8 format.

http://images6.fanpop.com/image/photos/38600000/Mountain-Goats-goats-38676825-1600-900.jpg|https://www.colorado.com/sites/default/master/files/CO_PhotoProject2007_0525.jpg|http://blog.seattletimes.nwsource.com/field_notes/facing.jpg|http://static2.techinsider.io/image/5633b71f9dd7cc25008c6247/mountain-goats-have-incredible-cliff-climbing-skills--heres-how-they-do-it.jpg|https://www.hcn.org/issues/45.19/can-rocks-and-paintballs-help-humans-and-mountain-goats-coexist/mountain-goats1-not-thumb-jpg/image