Aim: How to handle application server data.
Note 1: If defined structure element and its types all are in char (C), then we can use
ex:
OPEN DATASET l_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.
Upload data into txt file
REPORT ZSAMPLE_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data upload file location in SAP
TYPES : BEGIN OF ST_DEMO, "structure in data type C
REG_NO TYPE C,
NAME(20) TYPE C,
ADDR(20) TYPE C,
END OF ST_DEMO.
DATA : WA_DEMO TYPE ST_DEMO,
IT_DEMO TYPE TABLE OF ST_DEMO,
L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
WA_DEMO-REG_NO = '100003'.
WA_DEMO-NAME = 'ANAND'.
WA_DEMO-ADDR = 'NAGARKOVIL'.
APPEND WA_DEMO TO IT_DEMO.
WA_DEMO-REG_NO = '100005'.
WA_DEMO-NAME = 'VIKRAM'.
WA_DEMO-ADDR = 'CHENNAI'.
APPEND WA_DEMO TO IT_DEMO.
*CONCATENATE l_fname sy-datum sy-UZEIT INTO l_fname SEPARATED BY '_' . "optinal one if you want used it, or else leave it
OPEN DATASET L_FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC = 0.
LOOP AT IT_DEMO INTO WA_DEMO.
TRANSFER WA_DEMO TO L_FNAME.
ENDLOOP.
ENDIF.
_________________________________________________________________________________
Download data form txt file to internal table
REPORT ZSAMPLE1_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data downloaded file location in SAP
TYPES : BEGIN OF ST_DEMO, "structure in data type C
REG_NO TYPE C,
NAME(20) TYPE C,
ADDR(20) TYPE C,
END OF ST_DEMO.
DATA : WA_DEMO TYPE ST_DEMO,
IT_DEMO TYPE TABLE OF ST_DEMO,
L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
OPEN DATASET l_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.
if sy-subrc = 0.
DO.
READ DATASET l_fname INTO WA_DEMO.
IF sy-subrc = 0.
APPEND WA_DEMO TO IT_DEMO.
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDIF.
_________________________________________________________________________________
Delete txt file from application server
REPORT ZSAMPLE2_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data deleted file location in SAP
DATA : L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
DELETE DATASET l_fname.
_________________________________________________________________________________
Note 2: The defined structure element in multiple types , then we can use
ex:
OPEN DATASET l_fname FOR INPUT IN BINARY MODE ENCODING DEFAULT.
Upload data into txt file
REPORT ZSAMPLE_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data upload file location in SAP
TYPES : BEGIN OF ST_DEMO, "structure in data type with 'C' and 'num'
REG_NO TYPE num4,
NAME(20) TYPE C,
ADDR(20) TYPE C,
END OF ST_DEMO.
DATA : WA_DEMO TYPE ST_DEMO,
IT_DEMO TYPE TABLE OF ST_DEMO,
L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
WA_DEMO-REG_NO = '100003'.
WA_DEMO-NAME = 'ANAND'.
WA_DEMO-ADDR = 'NAGARKOVIL'.
APPEND WA_DEMO TO IT_DEMO.
WA_DEMO-REG_NO = '100005'.
WA_DEMO-NAME = 'VIKRAM'.
WA_DEMO-ADDR = 'CHENNAI'.
APPEND WA_DEMO TO IT_DEMO.
*CONCATENATE l_fname sy-datum sy-UZEIT INTO l_fname SEPARATED BY '_' . "optinal one if you want used it, or else leave it
OPEN DATASET L_FNAME FOR OUTPUT IN BINARY MODE. "major change between is marked in RED color
IF SY-SUBRC = 0.
LOOP AT IT_DEMO INTO WA_DEMO.
TRANSFER WA_DEMO TO L_FNAME.
ENDLOOP.
ENDIF.
Note 1: If defined structure element and its types all are in char (C), then we can use
ex:
OPEN DATASET l_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.
Upload data into txt file
REPORT ZSAMPLE_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data upload file location in SAP
TYPES : BEGIN OF ST_DEMO, "structure in data type C
REG_NO TYPE C,
NAME(20) TYPE C,
ADDR(20) TYPE C,
END OF ST_DEMO.
DATA : WA_DEMO TYPE ST_DEMO,
IT_DEMO TYPE TABLE OF ST_DEMO,
L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
WA_DEMO-REG_NO = '100003'.
WA_DEMO-NAME = 'ANAND'.
WA_DEMO-ADDR = 'NAGARKOVIL'.
APPEND WA_DEMO TO IT_DEMO.
WA_DEMO-REG_NO = '100005'.
WA_DEMO-NAME = 'VIKRAM'.
WA_DEMO-ADDR = 'CHENNAI'.
APPEND WA_DEMO TO IT_DEMO.
*CONCATENATE l_fname sy-datum sy-UZEIT INTO l_fname SEPARATED BY '_' . "optinal one if you want used it, or else leave it
OPEN DATASET L_FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC = 0.
LOOP AT IT_DEMO INTO WA_DEMO.
TRANSFER WA_DEMO TO L_FNAME.
ENDLOOP.
ENDIF.
_________________________________________________________________________________
Download data form txt file to internal table
REPORT ZSAMPLE1_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data downloaded file location in SAP
TYPES : BEGIN OF ST_DEMO, "structure in data type C
REG_NO TYPE C,
NAME(20) TYPE C,
ADDR(20) TYPE C,
END OF ST_DEMO.
DATA : WA_DEMO TYPE ST_DEMO,
IT_DEMO TYPE TABLE OF ST_DEMO,
L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
OPEN DATASET l_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.
if sy-subrc = 0.
DO.
READ DATASET l_fname INTO WA_DEMO.
IF sy-subrc = 0.
APPEND WA_DEMO TO IT_DEMO.
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDIF.
_________________________________________________________________________________
Delete txt file from application server
REPORT ZSAMPLE2_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data deleted file location in SAP
DATA : L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
DELETE DATASET l_fname.
_________________________________________________________________________________
Note 2: The defined structure element in multiple types , then we can use
ex:
OPEN DATASET l_fname FOR INPUT IN BINARY MODE ENCODING DEFAULT.
Upload data into txt file
REPORT ZSAMPLE_AL11.
"D:\usr\sap\DEV\DVEBMGS00\data upload file location in SAP
TYPES : BEGIN OF ST_DEMO, "structure in data type with 'C' and 'num'
REG_NO TYPE num4,
NAME(20) TYPE C,
ADDR(20) TYPE C,
END OF ST_DEMO.
DATA : WA_DEMO TYPE ST_DEMO,
IT_DEMO TYPE TABLE OF ST_DEMO,
L_FNAME TYPE STRING .
PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\DEV\SYS\src\ZSAMPLE.TXT' OBLIGATORY.
L_FNAME = P_FNAME.
WA_DEMO-REG_NO = '100003'.
WA_DEMO-NAME = 'ANAND'.
WA_DEMO-ADDR = 'NAGARKOVIL'.
APPEND WA_DEMO TO IT_DEMO.
WA_DEMO-REG_NO = '100005'.
WA_DEMO-NAME = 'VIKRAM'.
WA_DEMO-ADDR = 'CHENNAI'.
APPEND WA_DEMO TO IT_DEMO.
*CONCATENATE l_fname sy-datum sy-UZEIT INTO l_fname SEPARATED BY '_' . "optinal one if you want used it, or else leave it
OPEN DATASET L_FNAME FOR OUTPUT IN BINARY MODE. "major change between is marked in RED color
IF SY-SUBRC = 0.
LOOP AT IT_DEMO INTO WA_DEMO.
TRANSFER WA_DEMO TO L_FNAME.
ENDLOOP.
ENDIF.