Application Development and Automation 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: 
Read only

Reg: Working with internal table

Former Member
0 Likes
413

Hi,

I am creating a internal tablel like this.

TYPES :BEGIN OF TY_ITAB,

X TYPE Y,

Z TYPE A,

END OF ITAB.

DATA: ITAB TYPE TABLE OF TY_ITAB.

DATA: WA TYPE TY_ITAB.

CAN ANY ONE GIMME SOME EXAMPLE FOR INTERANL TABLE PROCESSING LIKE READ TABLE ,COLLECT AND ETC.

POINTS ASSURED.

Thanks and Regards,

Keny.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
394

Hi Arun,

I will try to explain with a simple example

Types: Begin of ty_itab,

x type string,

y type i,

end of itab.

data: itab type table of ty_itab,

wa type ty_itab.

Read is used to read a single record in the itab or to check whether the record is there or not.

wa-x = 'Test1'.

wa-y = 1.

append wa to itab.

wa-x = 'Test2'.

wa-y = 2.

append wa to itab.

Now when you do a read on itab.

read table itab into wa with key x = 'Test1'.

Now wa contains wa-x = test1 and wa-y = 1.

Collect is when you want to sum up all the records based on particular columns. This can be used only when all your columns are numeric except your key columns.

In this example

you already have an itab with 2 records and column X is considered as a Key

1st record

Test1 and 1

2nd record

Test2 and 2

Now you add 3rd record using collect statement.

First fill wa.

wa-x = 'Test1'.

wa-y = 3.

Now collect wa into itab.

Now as there is record whose x value is Test1. It will be added to that record.

Now you will have 2 records with changed values.

1st record

Test1 and 4 (This is because the new record numeric columns are be added to the record as their key is same)

2nd record

Test2 and 2

Now for e.g. you want add one more record

wa-x = 'Test3'

wa-y = 4

collect wa into itab.

Then you will have 3 records as there is no record which matches with the key of the new record ('Test3').

1st record

Test1 and 4

2nd record

Test2 and 2

3rd record

Test3 and 4

I hope my understanding is clear.

Thanks,

kiran

2 REPLIES 2
Read only

Former Member
0 Likes
395

Hi Arun,

I will try to explain with a simple example

Types: Begin of ty_itab,

x type string,

y type i,

end of itab.

data: itab type table of ty_itab,

wa type ty_itab.

Read is used to read a single record in the itab or to check whether the record is there or not.

wa-x = 'Test1'.

wa-y = 1.

append wa to itab.

wa-x = 'Test2'.

wa-y = 2.

append wa to itab.

Now when you do a read on itab.

read table itab into wa with key x = 'Test1'.

Now wa contains wa-x = test1 and wa-y = 1.

Collect is when you want to sum up all the records based on particular columns. This can be used only when all your columns are numeric except your key columns.

In this example

you already have an itab with 2 records and column X is considered as a Key

1st record

Test1 and 1

2nd record

Test2 and 2

Now you add 3rd record using collect statement.

First fill wa.

wa-x = 'Test1'.

wa-y = 3.

Now collect wa into itab.

Now as there is record whose x value is Test1. It will be added to that record.

Now you will have 2 records with changed values.

1st record

Test1 and 4 (This is because the new record numeric columns are be added to the record as their key is same)

2nd record

Test2 and 2

Now for e.g. you want add one more record

wa-x = 'Test3'

wa-y = 4

collect wa into itab.

Then you will have 3 records as there is no record which matches with the key of the new record ('Test3').

1st record

Test1 and 4

2nd record

Test2 and 2

3rd record

Test3 and 4

I hope my understanding is clear.

Thanks,

kiran

Read only

Former Member
0 Likes
394

Hi,

Here i am giving you the sample code for All internal table Related Operations.

REPORT z50871sd_rept_internaltable.

----


  • STRUCTURE DECLARATION

  • INTERNAL TABLE DECLARATION

  • WORK AREA DECLARATION

----


TYPES : BEGIN OF st_knc1,

kunnr TYPE knc1-kunnr, "Customer Number

bukrs TYPE knc1-bukrs, "Company Code

um01s TYPE knc1-um01s, "Total of the Debit Postings for the Month

END OF st_knc1.

DATA : it_knc1 TYPE STANDARD TABLE OF st_knc1,

wa_knc1 TYPE st_knc1 .

----


  • PARAMETER DECLARATION

----


PARAMETER p_x TYPE i. "NUMBER OF RECORDS

----


  • START-OF-SELECTION

----


START-OF-SELECTION.

SELECT kunnr

bukrs

um01s

FROM knc1

INTO TABLE it_knc1

UP TO p_x ROWS.

IF sy-subrc NE 0.

MESSAGE e000(z50871msg).

ENDIF.

----


  • APPEND

----


wa_knc1-kunnr = 2165.

wa_knc1-bukrs = 1500.

wa_knc1-um01s = '5000.00'.

APPEND wa_knc1 TO it_knc1.

wa_knc1-kunnr = 3165.

wa_knc1-bukrs = 2500.

wa_knc1-um01s = '5000.00'.

APPEND wa_knc1 TO it_knc1.

----


  • COLLECT

----


wa_knc1-kunnr = 2165.

wa_knc1-bukrs = 1500.

wa_knc1-um01s = '6000.00'.

COLLECT wa_knc1 INTO it_knc1.

CLEAR wa_knc1.

wa_knc1-kunnr = 4165.

wa_knc1-bukrs = 1500.

wa_knc1-um01s = '6000.00'.

COLLECT wa_knc1 INTO it_knc1.

CLEAR wa_knc1.

----


  • MODIFY

----


wa_knc1-kunnr = 6165.

wa_knc1-bukrs = 1600.

wa_knc1-um01s = '12000.50'.

MODIFY it_knc1 FROM wa_knc1 INDEX 1.

LOOP AT it_knc1 INTO wa_knc1 .

WRITE:/ wa_knc1-kunnr,

wa_knc1-bukrs,

wa_knc1-um01s CURRENCY 'INR'.

ENDLOOP.

----


  • INSERT

----


wa_knc1-kunnr = 5422.

wa_knc1-bukrs = 1200.

wa_knc1-um01s = '2000.50'.

INSERT wa_knc1 INTO it_knc1 INDEX 4.

SKIP 4.

WRITE : text-000 COLOR 1.

ULINE.

----


  • DELETE

----


DELETE it_knc1 INDEX 2.

DELETE ADJACENT DUPLICATES FROM it_knc1.

LOOP AT it_knc1 INTO wa_knc1 .

WRITE:/ wa_knc1-kunnr,

wa_knc1-bukrs,

wa_knc1-um01s CURRENCY 'INR'.

ENDLOOP.

----


  • Read

----


Read table it_knc1 into wa_knc1 index 10.

Regards

Sandeep Reddy