I want the solution to be like the following table:
---------------------
emp lft rgt
---------------------
Albert 1 12
Bert 2 3
Chuck 4 11
Donna 5 6
Eddie 7 8
Fred 9 10
--------------------
I have inserted above example values manually. But I need it dynamically
If I am going to insert one more entry in this table it may placed at the top, middle or end of the table. For example, if I insert Ragu reporting to Fred, then after this process, I need the result as follows:
---------------------
emp lft rgt
---------------------
Albert 1 14
Bert 2 3
Chuck 4 13
Donna 5 6
Eddie 7 8
Fred 9 12
Ragu 10 11
--------------------
I need the reorganization of the numbers to happen dynamically using a stored procedures.
Say you want to insert “Earl” 6 , 9
Up the left Value of anyone with a from range > 6.
Then update the corresponding “Rgt” for anyone with a value greater than +6.
The Insert the new row in the correct place.
INSERT INTO YOURTAB VALUES “Earl”,6, 9
A simpler more scalable approach would be to INSERT the initial values as multiples
of 100, then you can easily insert new values between the existing ranges. This would still require you to reorganize if you inserted a contiguous range of 6 new entries (150,125,112,106,103,101).
Or even better use floating point numbers for the range values then you will never need to reorganize as the will always be a number between the existing entries.
i.e. always a value
(Lft of row after - Lft of row before) / 2