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

Former Member
0 Kudos

Dear all

I'm importing a CSV file, using Open DataSet. But i'm facing a problem i can't split the comma Separator.

Can any one help me?

Best Regards

Thanks In advanced

PMR

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Pedro,

chk the program below...

REPORT Zxxy.

TYPE-POOLS:TRUXS.

DATA: BEGIN OF ITAB OCCURS 0,

VBELN LIKE VBAP-VBELN,

POSNR LIKE VBAP-POSNR,

END OF ITAB.

data: itabt like itab occurs 0 with header line.

DATA: ITAB1 TYPE TRUXS_T_TEXT_DATA,

ITAB2 TYPE TRUXS_T_TEXT_DATA.

SELECT VBELN

POSNR

UP TO 100 ROWS

FROM VBAP

INTO TABLE ITAB.

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'

EXPORTING

I_FIELD_SEPERATOR = ','

TABLES

I_TAB_SAP_DATA = ITAB

CHANGING

I_TAB_CONVERTED_DATA = ITAB1

EXCEPTIONS

CONVERSION_FAILED = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

FILENAME = 'C:\TEMP\test.txt'

TABLES

DATA_TAB = ITAB1

EXCEPTIONS

OTHERS = 1.

CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'

EXPORTING

I_FIELD_SEPERATOR = ','

I_TAB_RAW_DATA = ITAB1

  • I_FILENAME = 'C:\TEMP\test.txt'

TABLES

I_TAB_CONVERTED_DATA = ITABt

EXCEPTIONS

CONVERSION_FAILED = 1

OTHERS = 2

.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Reward points if you find this as a right solution

Regards,

Harini

5 REPLIES 5

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is one way to do it.



report zrich_0001.

parameters: d1 type localfile default '/usr/sap/TST/SYS/Data1.csv'.


data: itab type table of string with header line.
data: begin of idat occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      fld3(10) type c,
      end of idat.

start-of-selection.

  open dataset d1 for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset d1 into itab.
      if sy-subrc <> 0.
        exit.
      endif.
      append itab.
    enddo.
  endif.
  close dataset d1.

  loop at itab.

    clear idat.
    split itab at ',' into idat-fld1
                           idat-fld2
                           idat-fld3.
    append idat.
  endloop.

Regards,

Rich Heilman

former_member181966
Active Contributor
0 Kudos

Rich already answer it I dont want to cut and paste the code again and again !!

Message was edited by: Saquib Khan

Former Member
0 Kudos

Hi Pedro,

chk the program below...

REPORT Zxxy.

TYPE-POOLS:TRUXS.

DATA: BEGIN OF ITAB OCCURS 0,

VBELN LIKE VBAP-VBELN,

POSNR LIKE VBAP-POSNR,

END OF ITAB.

data: itabt like itab occurs 0 with header line.

DATA: ITAB1 TYPE TRUXS_T_TEXT_DATA,

ITAB2 TYPE TRUXS_T_TEXT_DATA.

SELECT VBELN

POSNR

UP TO 100 ROWS

FROM VBAP

INTO TABLE ITAB.

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'

EXPORTING

I_FIELD_SEPERATOR = ','

TABLES

I_TAB_SAP_DATA = ITAB

CHANGING

I_TAB_CONVERTED_DATA = ITAB1

EXCEPTIONS

CONVERSION_FAILED = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

FILENAME = 'C:\TEMP\test.txt'

TABLES

DATA_TAB = ITAB1

EXCEPTIONS

OTHERS = 1.

CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'

EXPORTING

I_FIELD_SEPERATOR = ','

I_TAB_RAW_DATA = ITAB1

  • I_FILENAME = 'C:\TEMP\test.txt'

TABLES

I_TAB_CONVERTED_DATA = ITABt

EXCEPTIONS

CONVERSION_FAILED = 1

OTHERS = 2

.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Reward points if you find this as a right solution

Regards,

Harini

Former Member
0 Kudos

first define a variable of type string.

data : x_data type string.

DO.

READ DATASET V_FILENAME INTO X_DATA.

IF SY-SUBRC = 0.

IF X_DATA IS NOT INITIAL.

SPLIT X_DATA AT ','

INTO X_INPUTFILE-KUNNR

X_INPUTFILE-ZGRP_CODE

X_INPUTFILE-MATNR.

*--give all your fields here.

APPEND X_INPUTFILE TO IT_INPUTFILE.

CLEAR X_INPUTFILE.

ENDIF.

ELSE.

EXIT.

ENDIF.

ENDDO.

your fields should be defined in internal table IT_INPUTFILE.

AFTER SPLIT, all values will be populated to your IT_INPUTFILE itab.

regards

srikanth

Former Member
0 Kudos

Hai

go through the following Code

data: splittab type table of string with header line.

data: begin of idat occurs 0,

fld1(10) type c,

fld2(10) type c,

fld3(10) type c,

end of idat.

open dataset file_name for input in text mode encoding default.

do.

read dataset file_name into splittab.

append splittab.

if sy-subrc ne 0.

close dataset file_name

exit.

endif

*do 8 times.

*get bit sy-index of v_hex into v_bit.

*write v_bit no-gap.

*enddo.

enddo.

loop at itab.

clear idat.

split itab at ',' into idat-fld1

idat-fld2

idat-fld3.

append idat.

endloop.

Thanks & regards

Sreeni