用於DataGrid MoveUp,MoveDown的存儲過程

所有邏輯都寫到了存儲過程中,所以在使用的時候,直接調用存儲過程即可,無需在前臺代碼中做過多的處理,但是一定要記得在綁定的sql語句中必須有order by orderno來排序

--測試表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Job_Person]'and OBJECTPROPERTY(id, N'IsUserTable'= 1)
drop table [dbo].[Job_Person]
GO

CREATE TABLE [dbo].[Job_Person] (
    
[Job_PersonID] [int] NOT NULL ,
    
[PersonID] [int] NOT NULL ,    
    
[OrderNo] [int] NULL ,    
    
[CreateOn] [datetime] NULL ,
    
[CreateByID] [int] NULL ,
    
[ModifyOn] [datetime] NULL ,
    
[ModifyByID] [int] NULL 
ON [PRIMARY]
GO

--測試存儲過程
CREATE PROCEDURE tp_MoveOrder_Entity_Job
(
         
@Job_EntityID        int        ,--表主鍵id
     @Direction         INT,        -- @Direction : move up : -1 , move down : 1
     @UserID         int        --修改人id
)  
AS
declare @EntityID         int
declare @OrderNo      INT
declare @MaxOrderNo Int

declare @OrderNo_1 int
declare @OrderNo_2 int

declare @Job_EntityID_1 int
declare @Job_EntityID_2 int

BEGIN

SET NOCOUNT ON

BEGIN 
      
      
Select @OrderNo = OrderNo, @EntityID = PersonID From Job_Person
                
Where Job_PersonID = @Job_EntityID

       
select @MaxOrderNo = max(OrderNo) From Job_Person
                
WHERE  PersonID = @EntityID

            
SET @OrderNo_1 = @OrderNo
            
SET @OrderNo_2 = @OrderNo + @Direction                     

        
if (@MaxOrderNo = 1 )
                
return 0    --只有一條記錄則返回            

        
if ((@OrderNo  = 1 ) AND (@Direction = -1))--設置move up orderno
        BEGIN
                
            
SET @OrderNo_1 = 1
            
SET @OrderNo_2 = @MaxOrderNo      
        
END

        
if ((@OrderNo  = @MaxOrderNo ) AND (@Direction = 1))--設置move down orderno
        BEGIN
                
            
SET @OrderNo_1 = 1
            
SET @OrderNo_2 = @MaxOrderNo      
        
END

        
--根據orderno取得不同的move資料
        select @Job_EntityID_1= Job_PersonID from Job_Person
        
where PersonID = @EntityID   and OrderNo = @OrderNo_2
        
        
select @Job_EntityID_2= Job_PersonID from Job_Person
        
where PersonID = @EntityID and OrderNo = @OrderNo_1 
    
--update orderno
    update Job_Person
    
set orderno = @OrderNo_1,
    ModifyOn 
= getdate(),
    ModifyByID 
= @UserID
    
where Job_PersonID = @Job_EntityID_1
     
IF @@ERROR <> 0
        
RETURN 1    
    
--update orderno
     update Job_Person
    
set orderno = @OrderNo_2,
    ModifyOn 
= getdate(),
    ModifyByID 
= @UserID
    
where Job_PersonID = @Job_EntityID_2
     
IF @@ERROR <> 0
        
RETURN 1         
END      

RETURN 0

END
GO
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章