Open Dataset in ABAP with Sample Program

Explain the definition of dataset.

The dataset contains the data for the query view.  Special applications require information that cannot be obtained using the method interfaces. For some applications you need more information than is contained in the cell contents. For example, you need the list of available characteristics or the contents of the dynamic filter before you are able to jump from one report into another.

Basic Form of the OPEN DATASET Statement 

To open a file on the application server, use the OPEN statement as follows:

OPEN DATASET <dsn> [Additions].

This statement opens the file <dsn>. 

If you do not specify any additions for the mode, the file is opened in binary mode for reading. SY-SUBRC returns 0 if the system opens the file. Otherwise, SY-SUBRC is set to 8.

You enter the filename <dsn> either as a literal or as a field containing the actual name of the file. If you do not specify a path, the system opens the file in the directory in which the R/3 System is running on the application server. To open a file, the user under which the R/3 System is running must have the requisite authorizations at operating system level. 

Sample Program for writing into internal table from Application Server

Since you want to upload from the application server, please make sure that your csv file is present in the right destination in the server by using t-code Al11.
Also to read from the application server use '/' and not '\'.
Also if you are using cg3z to upload the file to application server then please ensure that its in ACII format and not in binary format.

*------------------------------------------------------------------------------*
*          z_opendataset_input
*------------------------------------------------------------------------------*
report z_opendataset_input.

*------------------------------------------------------------------------------*
*          internal table declaration
*------------------------------------------------------------------------------*
DATA: BEGIN OF it_final OCCURS 0,
      matnr(10) TYPE c,
      werks(10) TYPE c,
      usage(10) TYPE c,
      data(08) TYPE c,
      END OF it_final.
*--- work area for the internal table
DATA wa_it_final LIKE LINE OF it_final.

*------------------------------------------------------------------------------*
*                    variable  declaration
*------------------------------------------------------------------------------*
DATA: v_excel_string(2000) TYPE c,
            v_file LIKE v_excel_string VALUE    'D:\TEST\TEST1.CSV',         " name of the file
             delimiter TYPE c VALUE ' '.                                                          " delimiter with default value space

*------------------------------------------------------------------------------*
*      read the file from the application server
*------------------------------------------------------------------------------*
  OPEN DATASET v_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc NE 0.
write:/ 'error opening file'.
  ELSE.
    WHILE ( sy-subrc EQ 0 ).
      READ DATASET v_file INTO wa_it_final.
      IF NOT wa_it_final IS INITIAL.
        APPEND wa_it_final TO it_final.
      ENDIF.
      CLEAR wa_it_final.
    ENDWHILE.
  ENDIF.
CLOSE DATASET v_file.

*------display the data from the internal table
LOOP AT IT_FINAL.

WRITE:/ IT_FINAL-MATNR, IT_FINAL-WERKS, IT_FINAL-USAGE,  IT_FINAL-DATA.

ENDLOOP.

ABAP

See Also:
Ways To Create Internal Table

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.