22,209
社区成员
发帖
与我相关
我的任务
分享
Select *,1 AS PAGE_NO from #T WHERE item_no=1
然后union all 下边是递归的关键,读取上一条的下一条数据,理解一下,就是读取item_no=1 的下一条数据,利用连续自增的item_no来处理,原表数据关联上一条已经读取的item_no=1的数据,取item_no=1数据+1的一条数据,不就是item_no=2的数据了么,下边就开始这个递归处理了item_no=3、item_no=4......--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([item_no] int,[AMOUNT_OF_PAGED] int)
Insert #T
select 1,81 union all
select 2,5 union all
select 3,5 union all
select 4,8 union all
select 5,11 union all
select 6,18 union all
select 7,1 union all
select 8,24 union all
select 9,11 union all
select 10,14
Go
--测试数据结束
;WITH cte AS (
Select *,1 AS PAGE_NO from #T WHERE item_no=1
UNION ALL
SELECT #T.*,cte.PAGE_NO+cte.AMOUNT_OF_PAGED FROM #T JOIN cte ON cte.item_no+1 = #T.item_no
)
SELECT * FROM cte
姓名 | 年龄 |
---|---|
<%=sqlRst.getString(1)%> | <%=sqlRst.getString(2)%> |