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

Insert/ Append into Itab having unique key

Karan_Chopra_
Active Participant
0 Likes
3,690

I have an Internal table(gi_sorted_material) with matnr as unique key but I have to insert values into it from another table(gi_material), but while inserting in loop it may be case that the gi_material may have multiple matnr with same number so i want that to be ignored while inserting , currently it gives runtime error or some syntax error.

I used this:

gi_sorted_material TYPE SORTED TABLE OF t_material WITH UNIQUE KEY matnr.

ls_sorted_material TYPE t_material.

READ TABLE gi_material INTO ls_material INDEX 1.

How to insert values from gi_material into gi_sorted_material

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,775

Hi,

Pls. read help for 'UNIQUE' , which says

"For a table key specified with UNIQUE,

a row with a certain key field content can only occur once in an internal table of this type" ...

So, it's not possible to insert another record with the same key.

Regards,

Srini.

5 REPLIES 5
Read only

Karan_Chopra_
Active Participant
0 Likes
1,775

Pllz reply

Read only

Former Member
0 Likes
1,776

Hi,

Pls. read help for 'UNIQUE' , which says

"For a table key specified with UNIQUE,

a row with a certain key field content can only occur once in an internal table of this type" ...

So, it's not possible to insert another record with the same key.

Regards,

Srini.

Read only

Former Member
0 Likes
1,775

Hi,

Try below code

gi_sorted_material TYPE SORTED TABLE OF t_material WITH UNIQUE KEY matnr.
ls_sorted_material TYPE t_material.

loop at gi_maternal into ls_material.
 read table gi_sorted_material into wa_sorted_material with key matnr = ls_material-matnr.
 if sy-subrc eq 0.
 modify gi_sorted_material from ls_material index sy-tabix.
else
append ls_material to gi_sorted_material.
endloop.

Regards

Vinod

Read only

Former Member
0 Likes
1,775

Hi,

Try This code,

loop at ls_sorted_material into wa.

insert wa into table gi_sorted_material."It will Not insert Duplicates of MATNR

endloop.

Regards,

Raghava Channooru

Read only

Former Member
0 Likes
1,775

Hi Karan,

If you want a unique record in your gi_sorted_material table then you can do 1 thing,

Just delete the duplicate records from the internal table gi_material comparing matnr.

SORT gi_material BY matnr.

DELETE ADJACENT DUPLICATES FROM gi_material COMPARING matnr.

This will give you all unique records in gi_material itself.

Then loop into gi_material and assign the records in gi_sorted_material according to your requirement.

Hope this helps.

Regards,

Vaibhav