日期:2014-05-18 浏览次数:20688 次
with t as (select row_number() over(order by TopFlag desc,IssueTime desc) rn,* from nInfoMain) select * from t where rn in ([指定的rn]-1,[指定的rn]+1)
------解决方案--------------------
declare @rid int select *,rid=identity(int,1,1) into #tb from nInfoMain order by TopFlag desc,IssueTime desc select @rid = rid from #tb where --你查询哪条数据的条件 select * from #tb where rid between @rid-1 and @rid+1
------解决方案--------------------
----------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-11-24 11:36:15
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[text] varchar(4),[TopFlag] int,[time] datetime)
insert [tb]
select 280,'内容',1,'2011-04-07 11:03:05' union all
select 164,'内容',1,'2011-03-07 11:03:05' union all
select 583,'内容',0,'2011-11-05 11:03:05' union all
select 251,'内容',0,'2011-06-11 11:03:05' union all
select 283,'内容',0,'2011-04-05 11:03:05' union all
select 211,'内容',0,'2011-03-24 11:03:05'
--------------开始查询--------------------------
declare @id int
set @id=164
;with f as
(
select px=ROW_NUMBER()over(order by getdate()),* from tb
)
select ID,TEXT,topflag,time from f where px=(select px from f where ID=@id)-1
union all
select ID,TEXT,topflag,time from f where px=(select px from f where ID=@id)+1
----------------结果----------------------------
/* ID TEXT topflag time
----------- ---- ----------- -----------------------
280 内容 1 2011-04-07 11:03:05.000
583 内容 0 2011-11-05 11:03:05.000
(2 行受影响)
*/