常用存储过程-用来获取某记录的上一条及下一条记录

功能:用于在指定条件中获取某记录的上一条及下一条记录。
用于:在记录详细信息页面显示某记录详细信息,并可单击上一条及下条快速浏览相关记录。

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
create PROCEDURE [ dbo ] . [ Sys_Common_GetPrivious_NextRecord ]
@currentId int ,
@strSql nvarchar ( 3000 )
AS
DECLARE @theSql nvarchar ( 3000 )
SET @theSql = ' declare RecordCursor cursor FOR ' + @strSql
EXEC sp_executesql @theSql
-- FOR EXAMPLE select id,dan FROM User Where dept=10002

declare @privious_Id int
declare @privious_Dan nvarchar ( 255 )
declare @next_Id int
declare @next_Dan nvarchar ( 255 )
declare @tempId int
DECLARE @tempDan nvarchar ( 255 )

set @privious_Id = 0
set @next_Id = 0
set @tempId = 0
SET @tempDan = ''

open RecordCursor
fetch next from RecordCursor into @tempId , @tempDan

while ( @@fetch_status = 0 )
BEGIN
if ( @tempId = @currentId )
BEGIN
fetch next from RecordCursor into @next_Id , @next_Dan
break
end
set @privious_Id = @tempId
SET @privious_Dan = @tempDan
fetch next from RecordCursor into @tempId , @tempDan
end
close RecordCursor
DEALLOCATE RecordCursor
select @privious_Id AS privious_Id, @privious_Dan AS privious_Dan, @next_Id AS next_Id, @next_Dan AS next_Dan

 

转载于:https://www.cnblogs.com/ruihua/archive/2010/08/08/1795186.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值