螳.. れ螻 螳 殊j鍵襯 蟆曙郁 .

T1011.0
T1032.0
T1043.0

1.0螻 2.0伎 伎 殊j鍵
T1011.0
T1021.5 <-- insert
T1032.0
T1043.0

覃 2.0覿 危覿 覈譟磯Μ 一危

T1011.0
T1022.0 <-- insert
T1033.0 <-- update
T1044.0 <-- update

覘.. 企 蟆曙郁 . 願 れ 企蟆 螻 覘螳 谿谿 . 企郁碓 Linked-List . winamp 覈襦 襯 企Μ襴 覦蠑碁 蟆曙磯. 一危郁 襷讌 る 一危碁ゼ 覦覯 蟯谿螻, 一危郁 襷る 伎 殊k 覦覯 蟯谿. 讌襷, CTE 蠏襯 伎 Linked List襯 蟲 覦覯 .


drop table #linked_list
create table #linked_list
(
    row_id int
,   next_row_id int
,   value int
)

--head, tail 誤, head tail row_id 
insert #linked_list 
values
    (0,9,0)
,   (9,9,0)

--3 曙
begin tran
    insert #linked_list 
    values 
        (1,9,3);

    update #linked_list
    set next_row_id = 1
    where row_id = 0;
commit;


--2 曙
begin tran
    insert #linked_list 
    values 
        (2,9,4);

    update #linked_list
    set next_row_id = 2
    where row_id = 1;
commit;


--譟壱
with cte
as
(
    select row_id, next_row_id, value, 1 order_val from #linked_list
    where row_id = 0
    union all
    select a.row_id, a.next_row_id, a.value, b.order_val + 1
    from #linked_list a
        inner join cte b
            on a.row_id = b.next_row_id
    where b.row_id < 9
)
select * from cte
order by order_val 

/*
row_id      next_row_id value       order_val
----------- ----------- ----------- -----------
0           1           0           1
1           2           3           2
2           9           4           3
9           9           0           4
*/