sqlserver获取当前id的前一条数据和后一条数据
select
*
from
tb
where
id=@id;
--当前记录
select
top
1 *
from
tb
where
id>@id
order
by
id;
--下一条记录
select
top
1 *
from
tb
where
id<@id
order
by
id
desc
--上一条记录
;
WITH
TUsers
AS
(
SELECT
*, ROW_NUMBER() OVER (
ORDER
BY
vcUserID)
AS
_ROW
FROM
DB_Fast_Users.dbo.T_Users
AS
A(NOLOCK)
)
SELECT
*
into
#T
FROM
TUsers
-- 上一条
select
B.*
from
#T
AS
A(NOLOCK)
left
join
#T
AS
B(NOLOCK)
ON
B._ROW = (A._ROW - 1)
where
A.vcUserID=
‘Test1‘
-- 下一条
select
B.*
from
#T
AS
A(NOLOCK)
left
join
#T
AS
B(NOLOCK)
ON
B._ROW = (A._ROW + 1)
where
A.vcUserID=
‘Test1‘
drop
table
#T
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。