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: 

internal tables

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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>.

25 REPLIES 25

former_member181962
Active Contributor
0 Kudos

&----


*& 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.

0 Kudos

You can use the following line to clear out all data in your internal table.

clear itab. refresh itab.

Regards,

Rich Heilman

Former Member
0 Kudos

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>.

0 Kudos

what does "occurs 0" mean? THANK YOU all! This is very helpful!!

0 Kudos

hi

with occurs 0 you are specifing that your table doesn't have a predefinited size (number of record)

bye

enzo

0 Kudos

The OCCURS systax is obselete in newer version and in ABAP objects. I would suggest starting to use the new syntax for creating internal tables.

Types: begin of ttab, 
       fld1 type c,
       fld2 type c,
       end of ttab.

data: itab type table of ttab.
data: wa type ttab.

Regards,

Rich Heilman

0 Kudos

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!

0 Kudos

Not sure. Go to SE37, enter ALSM_EXCEL_TO_INTERNAL_TABLE into the function module field and click display. This will tell you if it exists of not.

Regards,

Rich Heilman

0 Kudos

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?

0 Kudos

No, it does not exists in my sneak preview version.

Regards,

RIch Heilman

0 Kudos

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.

0 Kudos

Try using the function module GUI_UPLOAD.

Or the class/method, CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD.

They both exists in my sneak preview.

Regards,

Rich Heilman

0 Kudos

Good, I have that one -thanks! Do you happen to know the code for calling this? My ABAP book doesn't mention it.

0 Kudos

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

0 Kudos

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

0 Kudos

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.

Former Member
0 Kudos

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.

0 Kudos

I think that he is trying to upload from frontend, it just so happens that he is bringing from linux, hence the file path/name.

Regards,

Rich Heilman

Message was edited by: Rich Heilman

0 Kudos

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

0 Kudos

Fields names from the file? What fields names are you referring to?

Regards,

Rich Heilman

0 Kudos

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!!

0 Kudos

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

0 Kudos

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

0 Kudos

thanks! This is helpful.

0 Kudos

Make sure to award points for Helpful answers. Thanks.

Regards,

Rich Heilman