Sorted table
is another kind of index table which has unique / non unique key. It also can
be accessed via index or key. For sorted table the key must be specified. The
declaration is as follows:
DATA: it_mat TYPE
SORTED TABLE OF
ty_mat WITH UNIQUE KEY matnr,
it_mat TYPE
SORTED TABLE OF
ty_mat WITH NON-UNIQUE KEY matnr.
Unique key means the MATNR (material no) will must be
unique. If same material number is inserted then a run time error will happen.
However we can declare the sorted table with non unique key also. In this case
same material number can be entered but it will be sorted after entering the
number. Here the sorted table behaves similar to sorted standard table. We use
INSERT statement to enter any records to the sorted table.
INSERT wa_mat INTO
TABLE it_mat.
Here is an example of sorted table by using unique key
concept.
REPORT zabap_gui.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Sorted internal table with non unique key
DATA:
itab TYPE SORTED TABLE OF ty_tab WITH UNIQUE KEY item,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.
INSERT wtab INTO TABLE itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
INSERT wtab INTO TABLE itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
INSERT wtab INTO TABLE itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.
WRITE: /3 'Item',
13 'Quantity (KG)',
28 'Price (Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
SKIP.
WRITE '=========================================='.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Sorted internal table with non unique key
DATA:
itab TYPE SORTED TABLE OF ty_tab WITH UNIQUE KEY item,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.
INSERT wtab INTO TABLE itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
INSERT wtab INTO TABLE itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
INSERT wtab INTO TABLE itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.
WRITE: /3 'Item',
13 'Quantity (KG)',
28 'Price (Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
SKIP.
WRITE '=========================================='.
Since the key is unique the similar entries are ignored by the system.
Here is an example of sorted table by using non unique key
concept.
REPORT zabap_gui.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Standard internal table with non unique key
DATA:
itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.
INSERT wtab INTO TABLE itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
INSERT wtab INTO TABLE itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
INSERT wtab INTO TABLE itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.
WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
SKIP.
WRITE '=========================================='.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Standard internal table with non unique key
DATA:
itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.
INSERT wtab INTO TABLE itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
INSERT wtab INTO TABLE itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
INSERT wtab INTO TABLE itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.
WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
SKIP.
WRITE '=========================================='.
6 comments:
Hi Sir,
I have found your all blogs very useful.
In the above blog, you hvae mentioned
"If same material number is inserted then a run time error will happen."
There will not be any runtime error. Instead duplicate record will be ignored.
Please correct me if I am wrong.
Thanks & Regards,
Jaspal
Really very useful as information and presented way also very nice Sandip Roy
Informative Blog...For a long time I was craving for a career growth in programming and then I came to know that THE CREATING EXPERTS is the one who provide training with hands on training and real time scenarios
http://thecreatingexperts.com/sap-abap-training-in-chennai/
contact 8122241286
Thanks for your info...Here THE CREATING EXPERTS provide hands on training with real time scenarios
http://thecreatingexperts.com/sap-abap-training-in-chennai/
contact +91-08122241286
Hi, I learned SAP Training in Chennai from THE CREATING EXPERTS. The training was good and i got selected in leading MNC company as SAP Consultant.
contact 8122241286
www.thecreatingexperts.com
Thank you for posting the valuable information sap-hr online classes
Post a Comment