作者:xialaqimixyBo2_1940_321 | 来源:互联网 | 2023-05-18 12:55
一、条件字段为数值的情况
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