Sunday, May 22, 2011

Download data from tables with user defined delimiters

http://www.saptechnical.com/Docs/ABAP%20Code%20Sample%20for%20Download%20with%20User-Defined%20Delimiters.pdf

Download data from tables with user defined Delimiters
The data present in the tables has to be downloaded into some flat files for processing.
Normally we can download with a only a specified set of delimiters. In this program we
can download data to flat files with any user defined delimiter apart from the standard tab
delimiter.
The Source Code.
*----------------------------------------------------------------------*
* Report ZC1DOWNLOAD
*----------------------------------------------------------------------*
* Description : This Program helps to download data from tables
* into a flat with default as well as user defined
* delimiters.
* Program By : Kathirvel Balakrishnan.
* Created on : 24 December 2004.
* Transport Request: PDCK902348.
* Development Class: ZC1DEVCLASS.
* Transaction Code :
*----------------------------------------------------------------------*
REPORT zc1download message-id zc1dwnmsg.
*&---------------------------------------------------------------------*
*& Declaration Section for the Tables *
*&---------------------------------------------------------------------*
T
ABLES: makt.
*&---------------------------------------------------------------------*
*& Declaration Section for the Internal Tables
*&---------------------------------------------------------------------*
DATA: intab TYPE TABLE OF makt,
wa_intab LIKE LINE OF intab,
no_of_rec TYPE i,
count TYPE i.
D
ATA: BEGIN OF f_intab,
str(255) TYPE c,
END OF f_intab.
D
ATA: t_intab LIKE TABLE OF f_intab,
w_intab LIKE LINE OF t_intab,
temp(255) TYPE c.
FIELD-SYMBOLS: TYPE ANY.
*&---------------------------------------------------------------------*
*& Selection ScreenSection for the file download
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
P
ARAMETERS: file TYPE rlgrap-filename MEMORY ID file,
tab RADIOBUTTON GROUP rad1 DEFAULT 'X',
others RADIOBUTTON GROUP rad1,
delimit TYPE c.
S
ELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
IF file IS INITIAL. " check to ensure file.
MESSAGE i001.
EXIT.
ENDIF.
IF others = 'X'. " check to ensure delimiter.
IF delimit = ' '.
MESSAGE i002.
EXIT.
ENDIF.
ENDIF.
SELECT * FROM makt INTO TABLE intab.
IF tab = 'X'. " default delimiter tab is used
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
filename = file
filetype = 'DAT'
mode = 'A'
TABLES
data_tab = intab
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 sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ELSE. " If user defind delimiter is to be used
*
Counts the number of fields *
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE wa_intab TO .
IF sy-subrc <> 0.
EXIT.
ELSE.
count = count + 1.
ENDIF.
ENDDO.
LOOP AT intab INTO wa_intab.
DO count TIMES. " Adding the delimiter in required places
ASSIGN COMPONENT sy-index OF STRUCTURE wa_intab TO .
CONCATENATE temp delimit INTO temp.
ENDDO.
SHIFT temp.
APPEND temp TO t_intab.

CLEAR temp.
ENDLOOP.
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
filename = file
filetype = 'ASC'
mode = 'A'
TABLES
data_tab = t_intab
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 sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
WRITE:/ 'The Data has been tranfered to :', file.
Screen Shots – Tab Delimiter







No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

goodsites