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: 

Creating a text file output from ABAP

former_member586438
Participant
0 Kudos
12,921

Hi

I'm trying to create a comma delimited text file by using ABAP code

Could someone please help me with my coding in the following areas or is there better coding to do it:

1)Is there a way to move the entire Customer file KNA1 into my text file instead of doing it field by field.

2)How can I do the comma seperation correctly?

-


REPORT ZTEXTFILE.

data: lt_kna1 type table of kna1.

data: ls_kna1 type kna1.

data: lv_filename type string value 'ztest.txt'.

select * from kna1 into table lt_kna1.

open dataset lv_filename for output in text mode encoding default.

IF sy-subrc <> 0.

WRITE 😕 'ERROR'.

EXIT.

ENDIF.

loop at lt_kna1 into ls_kna1.

transfer ls_kna1-kunnr to lv_filename.

transfer ';' to lv_filename.

transfer ls_kna1-name1 to lv_filename.

transfer ';' to lv_filename.

endloop.

close dataset lv_filename.

WRITE 😕 'DONE'.

-


Many thanks

Gerhard

1 ACCEPTED SOLUTION

Former Member
0 Kudos
2,181

REPORT ZTEXTFILE.

data: lt_kna1 type table of kna1,

ls_kna1 type kna1,

lv_filename type string value 'ztest.txt',

lv_file type string.

select * from kna1 into table lt_kna1.

open dataset lv_filename for output in text mode encoding default.

IF sy-subrc ne 0.

WRITE 😕 'ERROR'.

EXIT.

else

loop at lt_kna1 into ls_kna1.

CONCATENATE ls_kna1-kunnr ls_kna1-name1 into lv_file separated by ','.

transfer lv_file to lv_lilename.

clear lv_file.

endloop.

close dataset lv_filename.

endif.

WRITE 😕 'DONE'.

Try this out, should do the trick. Also, may i suggest not using '*' in your select statement and fetch data only for the fields you wish to have in your text file after filtering using a where Claus. It would greatly improve performance.

Edited by: AJ Nayak on Feb 9, 2012 11:05 AM

7 REPLIES 7

Former Member
0 Kudos
2,181

Hi Gerhard ,

Try to concatenate all fields into a string with comma in seperation and then transfer string to your lv_filename.

DATA: data TYPE string. " LOCAL VARIABLE

LOOP AT lt_kna1 into ls_kna1.

CONCATENATE <fields to be in text file> INTO data SEPARATED BY ','.

TRANSFER data TO lv_filename.

ENDLOOP.

I think this will help.

Regards,

Benson

Former Member
0 Kudos
2,182

REPORT ZTEXTFILE.

data: lt_kna1 type table of kna1,

ls_kna1 type kna1,

lv_filename type string value 'ztest.txt',

lv_file type string.

select * from kna1 into table lt_kna1.

open dataset lv_filename for output in text mode encoding default.

IF sy-subrc ne 0.

WRITE 😕 'ERROR'.

EXIT.

else

loop at lt_kna1 into ls_kna1.

CONCATENATE ls_kna1-kunnr ls_kna1-name1 into lv_file separated by ','.

transfer lv_file to lv_lilename.

clear lv_file.

endloop.

close dataset lv_filename.

endif.

WRITE 😕 'DONE'.

Try this out, should do the trick. Also, may i suggest not using '*' in your select statement and fetch data only for the fields you wish to have in your text file after filtering using a where Claus. It would greatly improve performance.

Edited by: AJ Nayak on Feb 9, 2012 11:05 AM

0 Kudos
2,181

Hi AJ

That worked perfectly !

I need to dump all fields from kna1 into this text file

Is there not a quicker way than the concatenating of each field?

0 Kudos
2,181

Hi,

There is a better way than concatenating each fields. I request you do to a F1 help for the statement ASSIGN COMPONENT or to search in the forums. You will find a elegant way of doing this.

0 Kudos
2,181

Hi AJ

>

> That worked perfectly !

>

> I need to dump all fields from kna1 into this text file

> Is there not a quicker way than the concatenating of each field?

I am not completely sure, but as far as my experience goes, i have had to manually concatenate each and every field manually in similar situations.

Since you need all fields from the table, i could provide u with a tip to copy and paste field names quickly.

Tcode - SE11 >>> enter Table name >> Display >>> press CTRL + Y, your pointer should now change to a cross >> select from top field to the end, it will get highlighted.... you know the rest .... copy-paste and add your work area name to the beginning of each field . hope that helps.

0 Kudos
2,181

Keshav's way would be better actually. using Field Symbols.

0 Kudos
2,181

Thanks for the help!