2012 Feb 09 9:08 AM
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
2012 Feb 09 10:03 AM
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
2012 Feb 09 9:35 AM
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
2012 Feb 09 10:03 AM
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
2012 Feb 09 10:25 AM
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?
2012 Feb 09 10:30 AM
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.
2012 Feb 09 10:36 AM
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.
2012 Feb 09 10:38 AM
Keshav's way would be better actually. using Field Symbols.
2012 Feb 09 10:52 AM