I would suggest changing your table and adding another OrderNumber column, so in the end it will be easy to order in this column.
Although there may be problems with this approach:
1) You have an existing table and you need to set the values ββof the OrderNumber column. I think this part is simple. You can simply set the initial zero values ββand add a CURSOR, for example, moving through your records and increasing the number of your orders.
2) When a new row appears in your table, you need to change your OrderNumber, but here it depends on your specific situation. If you only need to add elements to the end of the list, you can set the new value as MAX + 1. In another situation, you can try writing TRIGGER when inserting new elements and invoke the same steps in step 1). This can greatly affect performance, so you should carefully study your architecture and possibly change this unusual design.
Ihor deyneka
source share