Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Open DATASET--how it works? URGENT!!

Former Member
0 Kudos

hi all experts,

I have made an internal table and was using GUI_DOWNLOAD function module to download from that internal table to a specified file. But now instead of GUI module i need to use OPEN DATASET condition to put my data into application server.

So am confused for its syntax, PLZ help!!

what i have is :

Open Dataset DIR_RSYN for output in Text mode.

Transfer i_tab2 to DIR_RSYN.

Close dataset DIR_RSYN.

DIR_RSYN being the directory and i_tab2 being the internal table so is it this way how to use this or not? coz some error is being displayed? PLZ HELP URGENT!

REWARDS would be provided!!

7 REPLIES 7

Former Member
0 Kudos

data : outrec(500).

if not i_tab2[] is initial.

OPEN DATASET DIR_RSYN FOR OUTPUT IN TEXT

MODE encoding DEFAULT.

endif.

loop at i_itab2.

concatenate i_itab2-field1 i_itab2-field2 ...

into outrec separated by space.

transfer outrec to DIR_RSYN.

clear outrec.

endloop.

Close dataset DIR_RSYN.

Former Member
0 Kudos

hi,

here is the sample code ..


*   Download internal table to Application server file(Unix)
DATA: e_file like rlgrap-filename value '/usr/sap/tmp/file.txt'.

  open dataset e_file for output in text mode.
  lOOP AT it_datatab......
    transfer it_datatab to e_file.
  ENDLOOP.

  close dataset e_file. 

Former Member
0 Kudos

Hi Puneet,

Following is the sytax for the open dataset:-

open dataset p_uxfile for input in text mode.

if sy-subrc = 0.

clear: wa_file_records.

read dataset p_uxfile into wa_file_records.

while sy-subrc = 0.

append wa_file_records to int_file_records.

clear wa_file_records.

read dataset p_uxfile into wa_file_records.

if sy-subrc ne 0.

  • Do nothing

endif.

endwhile.

message i369 with text-006.

else.

message e369 with text-005.

exit.

endif.

close dataset p_uxfile.

Reward points if useful.

Regards,

Shilpi

Former Member
0 Kudos

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING NON-UNICODE.

IF SY-SUBRC <> 0.

message e000 with 'Error on input file read'.

ENDIF.

DO .

read dataset p_file into it_upload .

if sy-subrc <> 0.

exit.

else.

append it_upload.

clear : it_upload.

endif.

ENDDO.

try this.

Regards,

madan.

Former Member
0 Kudos

You have to use open dataset, read dataset and close dataset command for that.

Ex:



DATA: file TYPE string VALUE `flights.dat`, 
      wa TYPE spfli. 

FIELD-SYMBOLS <hex_container> TYPE x. 

OPEN DATASET file FOR INPUT IN BINARY MODE. 

ASSIGN wa TO <hex_container> CASTING. 

DO. 
  READ DATASET file INTO <hex_container>. 
  IF sy-subrc = 0. 
    WRITE: / wa-carrid, 
             wa-connid, 
             wa-countryfr, 
             wa-cityfrom, 
             wa-cityto, 
             wa-fltime, 
             wa-distance. 
  ELSE. 
    EXIT. 
  ENDIF. 
ENDDO. 

CLOSE DATASET file. 

Former Member

Hi,

datasets are the file son the application server.

open dataset opens the file for reading / writing or appending records.

read dataset reads the data from teh file to the work area / variables of the program

close dataset closes the file on the application server.

Open Dataset is used to read or write on to application server ... other than that i am not sure that there exists any way to do the same ... here is a short description for that

FILE HANDLING IN SAP

Introduction

• Files on application server are sequential files.

• Files on presentation server / workstation are local files.

• A sequential file is also called a dataset.

Handling of Sequential file

Three steps are involved in sequential file handling

• OPEN

• PROCESS

• CLOSE

Here processing of file can be READING a file or WRITING on to a file.

OPEN FILE

Before data can be processed, a file needs to be opened.

After processing file is closed.

Syntax:

OPEN DATASET <file name> FOR {OUTPUT/INPUT/APPENDING}

IN {TEXT/BINARY} MODE

This statement returns SY_SUBRC as 0 for successful opening of file or 8, if unsuccessful.

OUTPUT: Opens the file for writing. If the dataset already exists, this will place the cursor at the start of the dataset,

the old contents get deleted at the end of the program or when the CLOSE DATASET is encountered.

INPUT: Opens a file for READ and places the cursor at the beginning of the file.

FOR APPENDING: Opens the file for writing and places the cursor at the end of file. If the file does not exist, it is generated.

BINARY MODE: The READ or TRANSFER will be character wise. Each time ‘n’’ characters are READ or transferred.

The next READ or TRANSFER will start from the next character position and not on the next line.

IN TEXT MODE: The READ or TRANSFER will start at the beginning of a new line each time.

If for READ, the destination is shorter than the source, it gets truncated.

If destination is longer, then it is padded with spaces.

Defaults: If nothing is mentioned, then defaults are FOR INPUT and in BINARY MODE.

PROCESS FILE:

Processing a file involves READing the file or Writing on to file TRANSFER.

TRANSFER Statement

Syntax:

TRANSFER <field> TO <file name>.

<Field> can also be a field string / work area / DDIC structure.

Each transfer statement writes a statement to the dataset. In binary mode, it writes the length of the field to the dataset.

In text mode, it writes one line to the dataset.

If the file is not already open, TRANSFER tries to OPEN file FOR OUTPUT (IN BINARY MODE) or using the last OPEN DATASET statement for this file.

IF FILE HANDLING, TRANSFER IS THE ONLY STATEMENT WHICH DOES NOT RETURN SY-SUBRC

READ Statement

Syntax:

READ DATASET <file name> INTO <field>.

<Field> can also be a field string / work area / DDIC structure.

Each READ will get one record from the dataset.

In binary mode it reads the length of the field and in text mode it reads each line.

CLOSE FILE:

The program will close all sequential files, which are open at the end of the program.

However, it is a good programming practice to explicitly close all the datasets that were opened.

Syntax:

CLOSE DATASET <file name>.

SY-SUBRC will be set to 0 or 8 depending on whether the CLOSE is successful or not.

DELETE FILE:

A dataset can be deleted.

Syntax:

DELETE DATASET <file name>.

SY-SUBRC will be set to 0 or 8 depending on whether the DELETE is successful or not.

Pseudo logic for processing the sequential files:

For reading:

Open dataset for input in a particular mode.

Start DO loop.

Read dataset into a field.

If READ is not successful.

Exit the loop.

Endif.

Do relevant processing for that record.

End the do loop.

Close the dataset.

For writing:

Open dataset for output / Appending in a particular mode.

Populate the field that is to be transferred.

TRANSFER the filed to a dataset.

Close the dataset.

Regards,

Raj.

Former Member

DATA:

lv_file TYPE string.

IF p_rapps EQ gc_x AND p_apps IS INITIAL.

  • //text-060: Please enter application server file path

MESSAGE e000(zhrgeneral) WITH text-060.

ELSEIF p_rpres EQ gc_x AND p_pres IS INITIAL.

  • // Please enter presentation server file path

MESSAGE e028.

ENDIF.

CONCATENATE p_apps text-004 sy-datum sy-uzeit text-005

INTO lv_file.

CONDENSE lv_file.

OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF NOT sy-subrc IS INITIAL.

  • //text-002: Please enter valid App. Server Path

MESSAGE s000(zhrgeneral) WITH text-002.

EXIT.

ELSE.

CLOSE DATASET lv_file. "Close the temporary file

DELETE DATASET lv_file. "Delete the temporary file

ENDIF.

Cheers,

Raadha