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

After INSERTING entry into database table, the table is not sorted by keys

Former Member
0 Likes
3,735

Does anybody know why when I insert a record into a database table from workarea, the record is inserted into the database table but the table is no longer sorted by the keys? The workarea is defined using:

<i>data: wa type dbtable.</i>

After populating the fields in wa:

<i>insert dbtable from wa</i>

The key for database table is a sequencial counter number <b>(INT4)</b>. For instance, after the insert, counter number equals to 10 is inserted into the dbtable <u>between</u> 8 and 9 instead of <u>after</u> 9. (Table content is displayed thru SE16) Also, I realized that all other fields besides the key, are sorted and according to that sorting, entry with counter number 10 is supposed to be between #8 and #9 but theologically if the database table is supposed to be sorted by key fields, shouldn't the entry be inserted after #9?

Any advice is appreciated. Thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,017

Hi guys,

Thanks for all the responses. Apparently our requirements is to use SE16 to display the table entry and determine which records has problem based on the sequence number, i.e. the key field. Thus, the table entry displayed in SE16 has to be sorted in the key field. There is a work around by using the "sorting" function in SE16 everytime when displaying the table entry but it is not the first preference.

With looking at the situation on how the table entries are appearing on SE16 after being inserted by the INSERT command, it is always appearing in SE16 that the table is 'sorted' order of the non-primary key fields. We are also trying to debug the program of SE16 to see if there is any special 'sorting' before it displayed the data onto the screen.

Again, thanks for the responses.

Does anybody know why when I insert a record into a database table from workarea, the record is inserted into the database table but the table is no longer sorted by the keys? The workarea is defined using:

<i>data: wa type dbtable.</i>

After populating the fields in wa:

<i>insert dbtable from wa</i>

The key for database table is a sequencial counter number <b>(INT4)</b>. For instance, after the insert, counter number equals to 10 is inserted into the dbtable <u>between</u> 8 and 9 instead of <u>after</u> 9. (Table content is displayed thru SE16) Also, I realized that all other fields besides the key, are sorted and according to that sorting, entry with counter number 10 is supposed to be between #8 and #9 but theologically if the database table is supposed to be sorted by key fields, shouldn't the entry be inserted after #9?

Any advice is appreciated. Thank you.

8 REPLIES 8
Read only

Former Member
0 Likes
2,017

Minami,

I am not clear what exactly you are expecting. Are you expecting to see the data in the database table to be sorted? If that is the case, I don't think that would be the case.

When ever you fetch the data into a internal table, you can fetch that into a sorted internal table and that will SORTED. The data inside the database table will NOT be sorted.

Regards

Ravi

Note : Please reward the posts that help you.

Read only

Former Member
0 Likes
2,017

Hi Minami,

I think what you should do is do a select on the table and see if you get it in the sorted order. For all you know, SE16 or SM30 is only a GUI to view the DB entries. Even if this gives it to you in sorted order, that doesnt gurantee in what order it is stored in the DB.

And how does the order in which it is stored in the DB matter anyway ?

Pls reward points and close this thread, if your Q is answered.

Rgds,

Prabhu.

Read only

former_member186741
Active Contributor
0 Likes
2,017

Hi Minami,

please give us the table layout (indicating key fields) and list the first few entries to demonstrate how the 'sort' is occurring and indicate how you would expect it to be. Does the table have a MANDT before your key field?

Regards

Neil

Read only

Former Member
0 Likes
2,017

Hi Minami,

1. Welcome to SDN.

2. Even though a table has a primary key,

it does not mean that after inserting a record

in a database table,

then after viewing it in SE16 / SE11

it will appear in a sorted manner !!!

3. No, it won't appear sorted.

(Try any table, for eg. MARA)

4. The DATABASE stores the records,

in its own fashion.

While retireiving thru SQL, however,

we can specify the sorting order.

5. Since u are new to the forum, let me introduce

to the forum etiquette.

If u find any answer useful, u may award

points by clicking on the STAR which appears

to the left of the reply .

U may please award points to the repliy(s)

which u found useful.

Regards,

Amit M.

Read only

Former Member
0 Likes
2,018

Hi guys,

Thanks for all the responses. Apparently our requirements is to use SE16 to display the table entry and determine which records has problem based on the sequence number, i.e. the key field. Thus, the table entry displayed in SE16 has to be sorted in the key field. There is a work around by using the "sorting" function in SE16 everytime when displaying the table entry but it is not the first preference.

With looking at the situation on how the table entries are appearing on SE16 after being inserted by the INSERT command, it is always appearing in SE16 that the table is 'sorted' order of the non-primary key fields. We are also trying to debug the program of SE16 to see if there is any special 'sorting' before it displayed the data onto the screen.

Again, thanks for the responses.

Read only

0 Likes
2,017

You can acheive the turn around functionality by writing a simple abap program which selects the required data from the database and displays the records in the sorted order as you require. i think this will be easier than debugging the entire SE16 transaction and then searching for the exit to put the desired sort statement.

Hope this helps.

Read only

0 Likes
2,017

Hi

I think the problem could be on not key fields:

I've try to do this:

- Created a new Z-TABLE with only fied: the key field as INT4: after inserting a new records, the SE16 show me the record correctly sorted;

- Added a new char fields in my Z-TABLE: after inserting some values in new fields, the SE16 show me the record sorted for new (char) fields and not for key field.

I think the problem it's when SE16 sorts the table, becouse it uses this command SORT IZ-TABLE (where IZ-TABLE is an internal table like Z-TABLE).

The command SORT <TABLE> (without specify the fields to sort) sorts the internal table by the key fields, but the key fields of internal table are only the char fields.

So it should be better the numeric key field of your table is a char numeric field.

Probably for this reason, in all SAP tables with a numeric key field, the key field is always a char field.

Max

Message was edited by: max bianchi

Message was edited by: max bianchi

Read only

0 Likes
2,017

Max,

That solved the problem!! Thank you.

We changed the INT4 to NUMC and the table is sorted correctly by the key field after the insert.

We are happy now! Thanks a lot!

Minami