Export SAP internal table to pipe-delimited text

Jimbo's picture

/image.php?image=misc/pipegoatbig.jpg|/image.php?image=misc/pipegoat.jpg|https://s-media-cache-ak0.pinimg.com/originals/9f/83/4a/9f834a995d8a7ae67986ac887c53112c.jpgIn the 21st century, it is still possible to come across the requirement to pass data in the form of pipe-delimited text. The pipe or ¦ character (ASCII code 166) is preferred by many programmers because it exists in all operating systems on any system and occurs very infrequently in data.

It is absolutely possible to export to pipe-delimited text from SAP and it is really simple. The simple solution is to concatenate all the fields into a string and then append the string to a table.

Start by declaring a table to hold the concatenated fields. The string type is fine, but a long-enough text field will work fine, too.

data : begin of i_text occurs 0,
       text(1024) type c,
       end of i_text.

Next, use the concatenate to put all the fields, pipe-delimited, into a work area.

concatenate wa_R-BUTXT '|' wa_R-SubUnit
  '|' wa_R-First '|' wa_R-Last '|' wa_R-Middle '|' wa_R-Email
  '|' wa_R-LIFNR '|' wa_R-BUKRS '|' wa_R-Dept
  '|' wa_R-LIFNRA '|' wa_R-EMAILB into i_text.
append i_text. 

Finally, export the internal table to a text file. The WRITE_FIELD_SEPARATOR need not be written because the pipe is the separator and there is only one field.

If the format is to be UTF-8 then be sure to set the CODEPAGE to '4310'. If no code page is provided then the function defaults to ASCII.

CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      FILENAME                        = p_File
      FILETYPE                        = 'ASC'
*       APPEND                          = lv_Append
       CODEPAGE                        = '4310'
      WRITE_FIELD_SEPARATOR = ' '
      HEADER = '00'
      TRUNC_TRAILING_BLANKS = 'X'
    TABLES
*      DATA_TAB                        = it_R
       DATA_TAB                       = i_text
    EXCEPTIONS
      FILE_WRITE_ERROR                = 1
      OTHERS                          = 2.

Programming Language: 
ABAP