‎2005 Nov 21 1:40 PM
Hi,
Can someone please give me the code for initializing an internal table. Also, some code showing how to add data to or extract data from the table would also be helpful. The table needs to have two fields that are string and another that is long integer.
thanks-
Kevin
‎2005 Nov 21 1:48 PM
data: begin of itab ocuurs 0,
field1 like tab1-field1,
field2 like tab1-field2,
end of itab.
clear: itab[],
itab.
select field1 field2 from itab into table itab
where <condition1> and <condition2>.
‎2005 Nov 21 1:46 PM
&----
*& Module pool YRT_TEST1
*&
&----
*&
*&
&----
PROGRAM YRT_TEST1.
*parameters : date1 like sy-datum,
date2 like sy-datum.
*data: days(3) type n.
*data: years(10) type p decimals 3.
*
*start-of-selection.
*days = date2 - date1.
*
*years = days / 365.
*
*write: / days,
years.
data: begin of itab occurs 0,
str type string,
inte type i,
end of itab.
initialization.
itab-str = 'My name'.
itab-inte = 12.
append itab.
clear itab.
itab-str = 'My address'.
itab-inte = 13.
append itab.
clear itab.
Start-of-selection.
*adding data
itab-str = 'additional data'.
itab-inte = 15.
append itab.
clear itab.
*extracting data
loop at itab.
write:/ itab-str,
itab-inte.
endloop.
‎2005 Nov 21 1:48 PM
‎2005 Nov 21 1:48 PM
data: begin of itab ocuurs 0,
field1 like tab1-field1,
field2 like tab1-field2,
end of itab.
clear: itab[],
itab.
select field1 field2 from itab into table itab
where <condition1> and <condition2>.
‎2005 Nov 21 2:09 PM
what does "occurs 0" mean? THANK YOU all! This is very helpful!!
‎2005 Nov 21 2:10 PM
hi
with occurs 0 you are specifing that your table doesn't have a predefinited size (number of record)
bye
enzo
‎2005 Nov 21 2:13 PM
‎2005 Nov 21 2:26 PM
Okay, thanks.
One more question... I just ran my program and got:
The function module "ALSM_EXCEL_TO_INTERNAL_TABLE" was called, but cannot be found in the function library.
Does the NW sneak preview on Linux have this function included? I copied the Excel file to Linux and attempted to import it to my internal table.
thanks!
‎2005 Nov 21 2:28 PM
‎2005 Nov 21 2:32 PM
Does not exist. Does it exist on the NW sneak preview for windows? I need to get off Linux somehow, but I cannot change the name of my laptop on the Windows side. Is there some work-around that allows you to install the free Netweaver without changing the computer name?
‎2005 Nov 21 2:42 PM
‎2005 Nov 21 2:53 PM
Wow, so the Windows sneak preview does not have this function module? Am I trying to import data using deprecated code? All I'm trying to do, really, is import data into SAP, preferably from an Excel table.
‎2005 Nov 21 2:55 PM
‎2005 Nov 21 3:06 PM
Good, I have that one -thanks! Do you happen to know the code for calling this? My ABAP book doesn't mention it.
‎2005 Nov 21 3:09 PM
DATA: g_file TYPE string.
DATA: i_dataf TYPE char2000 OCCURS 0 WITH HEADER LINE.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = g_file
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = ' '
HEADER_LENGTH = 0
READ_BY_LINE = 'X'
IMPORTING
FILELENGTH =
HEADER =
TABLES
data_tab = i_dataf
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17
.
IF sy-subrc <> 0.
*error message
ENDIF.
Alternatively you can call any Function module by pressing the Pattern button on the appl tool bar in se38 editor. and give the FM name.
It will give the code for you.
You need to uncomment the parameters which you need to pass.
If you want to call a method belonging to a class, the use the pattern button. select the ABAP Object patterns radio-button,
Give the class/interface name and the method name.
This will give you semi-finished code, for which you need to pass the required parameters.
Message was edited by: Ravi Kanth Talagana
‎2005 Nov 21 3:13 PM
Sure.....I would suggest using comma delimited text files here. Once you have them uploaded to the flat itab, then you can loop thru ITAB and SPLIT the record up into its individual fields using the SPLIT statement.
report zrich_0001
no standard page heading.
types: begin of ttab,
rec(1000) type c,
end of ttab.
parameters: p_file type localfile.
data: itab type table of ttab.
data: file_str type string.
file_str = p_file.
call function 'GUI_UPLOAD'
exporting
filename = file_str
tables
data_tab = itab
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
* - OR -
call method cl_gui_frontend_services=>gui_upload
exporting
filename = file_str
changing
data_tab = itab
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
Please remember to award points for helpful answers. Thanks.
Regards,
Rich HEilman
‎2005 Nov 21 4:19 PM
I cannot get this code to work... any ideas? The error is "cannot interpret data in file" and then it takes me completely out of the abap editor! How do I modify the code so when I hit the green check, it takes me back to the code?
My text file is simply this (tab delimited):
Joe Bloggs 25
Dave Smith 36
Jane Walker 23
Code:
REPORT Z_SECOND_PROGRAM.
*Data Declarations
*Declare the internal table data structure
data: begin of itab occurs 0,
str1 type string,
str2 type string,
int1 type i,
end of itab.
data: g_file TYPE string.
*Initialize the table
initialization.
itab-str1 = 'Kevin was here'.
itab-str2 = 'Another string'.
itab-int1 = 23.
append itab.
*Display the table
loop at itab.
write:/ itab-str1,
itab-str2,
itab-int1.
endloop.
clear itab. refresh itab.
call function 'GUI_UPLOAD'
EXPORTING
filename = '/root/datafile1'
*HAS_FIELD_SEPARATOR='|'
TABLES
data_tab = itab
EXCEPTIONS
file_open_error=1
file_read_error=2
no_batch=3
gui_refuse_filetransfer=4
invalid_type=5
no_authority=6
unknown_error=7
bad_data_format=8
header_not_allowed=9
separator_not_allowed=10
header_too_long=11
unknown_dp_error=12
access_denied=13
dp_out_of_memory=14
disk_full=15
dp_timeout=16
OTHERS=17
.
IF sy-subrc <> 0.
*error message
ENDIF.
*Redisplay the table
loop at itab.
write:/ itab-str1,
itab-str2,
itab-int1.
endloop.
‎2005 Nov 21 5:31 PM
Kevin,
Please do not use the FM's with name GUI* to interact with the application server. As the name clearly suggests, GUI_UPLOAD or the method suggested by Rich are to upload data from the local system.
Regards,
Kalidas.
‎2005 Nov 21 5:33 PM
‎2005 Nov 21 7:04 PM
Rich,
I just recopied your code and got it to work. I needed to add "with header line" to itab. It does not display the field names when I print using:
loop at itab.
write / itab.
endloop.
Do you know how to do so?
thanks,
Kevin
‎2005 Nov 21 7:07 PM
‎2005 Nov 21 7:15 PM
Sorry, I'm still new at abap. I'm assuming there is a header line [which I want printed as well as the data] since it would not process without adding as follows:
data: itab type table of ttab with header line.
But there is no header line that is printed, only the data:
loop at itab.
write / itab.
endloop.
Also, how do I call the split function? I think this method of just using one field and then splitting it is a good one. I will need to be able to pass the resultant data to FV50... can FV50 (or any txn code for that matter) be called from within a program? If so, what is the syntax? Thanks!!
‎2005 Nov 21 7:21 PM
Specify "with HEader line" for your internal table has nothing to do with printing of the fields. It is used as a work area, a place for the data which is being read from the internal table to be stored. You could have also used a explicit work area.
data: wa like line of itab.
loop at itab into wa.
write:/w a.
endloop.I will be back with the split functionality.
Please make sure to award points for helpful answers. Thanks.
Regards,
Rich Heilman
‎2005 Nov 21 7:27 PM
Here is a sample program, it uploads the comma delimited file into a flat itab. Then loops at that itab splitting it up at the comma into fields of another internal table, then appends it to the internal table.
report zrich_0001.
types: begin of ttab,
rec(1000) type c,
end of ttab.
types: begin of tdat,
fld1(10) type c,
fld2(10) type c,
fld3(10) type c,
end of tdat.
data: itab type table of ttab with header line.
data: idat type table of tdat with header line.
data: file_str type string.
parameters: p_file type localfile.
at selection-screen on value-request for p_file.
call function 'KD_GET_FILENAME_ON_F4'
exporting
static = 'X'
changing
file_name = p_file.
start-of-selection.
file_str = p_file.
call function 'GUI_UPLOAD'
exporting
filename = file_str
tables
data_tab = itab
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
loop at itab.
clear idat.
split itab-rec at ',' into idat-fld1
idat-fld2
idat-fld3.
append idat.
endloop.
loop at idat.
write:/ idat-fld1, idat-fld2, idat-fld3.
endloop.
Regards,
Rich Heilman
‎2005 Nov 21 7:31 PM
thanks! This is helpful.
‎2005 Nov 21 7:36 PM