Common stored procedures-used to obtain the previous and next records of a record

Source: Internet
Author: User

Function: used to obtain the first and next records of a record in a specified condition.
Used to: display the detailed information of a record on the record details page, and click the previous and next record to view the relevant records quickly.

 

Code

 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.