且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在sql查询中使用表变量实现上一条下一条记录

更新时间:2022-08-18 22:08:32

在sql查询中使用表变量实现上一条下一条记录
在sql查询中使用表变量实现上一条下一条记录
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
--
 Author:        <Author,,Name>
--
 Create date: <Create Date,,>
--
 Description:    <Description,,>
--
 =============================================
alter PROCEDURE [dbo].PROG_Order_Main_GetNextPre
@OrderID nvarchar(30),
@type int
AS
BEGIN    
    
declare
    
        
@result table(
            id 
int identity(1,1),
            OrderID 
nvarchar(30not null
        )
    
declare
        
@select nvarchar(400),
        
@id int
    
insert into @result select OrderID from dbo.Order_Main order by AddTime desc
    
--1上一条
    --2下一条
    select @id=id from @result where OrderID=@OrderID
    
if(@type=1)
        
set @id=@id-1
    
else    
        
set @id=@id+1
    
select OrderID,AddTime,OrderType,Price_order from Order_Main where OrderID=(select OrderID from @result where id=@id)
END
GO
在sql查询中使用表变量实现上一条下一条记录

 

本文转自火地晋博客园博客,原文链接:http://www.cnblogs.com/yelaiju/archive/2010/06/14/1758395.html,如需转载请自行联系原作者