table title
id name
1 2
2 3table line
id title_id name key_index
1 1 A 2
2 1 B 4
3 2 C 1
用row_number()函数根据key_index顺序,注意此字段不连续、
出现如下结果
line_id name row_name
1 A 1
2 B 2
3 C 1
id name
1 2
2 3table line
id title_id name key_index
1 1 A 2
2 1 B 4
3 2 C 1
用row_number()函数根据key_index顺序,注意此字段不连续、
出现如下结果
line_id name row_name
1 A 1
2 B 2
3 C 1
from line
declare @line table (id int,title_id int,name varchar(1),key_index int)
insert into @line
select 1,1,'A',2 union all
select 2,1,'B',4 union all
select 3,2,'C',1select id,name,row_number() over(partition by title_id order by id) as row_name
from @line
/*
id name row_name
----------- ---- --------------------
1 A 1
2 B 2
3 C 1
*/
insert into @Table
select 1,1,'A',2 union all
select 2,1,'B',4 union all
select 3,2,'C',1 select line_ID,name,row_name=row_number() over(partition by title_id order by key_index)
from @Table
/*
line_ID name row_name
----------- ---- --------------------
1 A 1
2 B 2
3 C 1
id,name,row_number() over(partition by title_id order by id) as row_name
from
tb