SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.MoveSequence
(
@TableName nvarchar( 200 ),
@ID uniqueidentifier,
@isUp bit = 0
)
AS
/*
Yinping
2005-06-17
To move the item's sequence up or down
@TableName the item's table
@ID the item's Id
@isUp the move action '1' up ; '0' down
*/
Set @TableName = '[' + @TableName + ']'
Declare @ExecuteString nvarchar(1000)
Declare @itemSequence float
Declare @Sequence float
Declare @rowCount int
-- get its sequence
Set @ExecuteString ='Select @Sequence = Sequence From ' + @TableName + ' Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @Sequence float output',
@TableName,@ID,@itemSequence output
-- judge the move direction
if( @isUp = 1 )
-- move up
Begin
-- judge the item is the biggest
Set @ExecuteString = 'Select @rowCount = count(*) From ' + @TableName + ' Where Sequence >' + Cast( @itemSequence As Nvarchar(100) )
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@rowCount int output, @itemSequence float ',
@TableName,@rowCount output,@itemSequence
if( @rowCount = 0 )
Begin
-- it is the biggest
-- judge if existing more than one biggest
Set @ExecuteString = 'Select @rowCount = count(*) From ' + @TableName + ' Where Sequence =' + Cast( @itemSequence As Nvarchar(100) )
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@rowCount int output, @itemSequence float ',
@TableName,@rowCount output,@itemSequence
if( @rowCount > 1)
Begin
-- more than one the biggest ,to add its sequence
Set @ExecuteString ='Update ' + @TableName + ' Set Sequence = (@itemSequence + 1) Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @itemSequence float ',
@TableName,@ID ,@itemSequence
return
End
Else
Begin
-- only one biggest
return
End
End
Else if( @rowCount = 1 )
Begin
-- only have one item bigger than it ,meaning it is the bigger
-- get the biggest's sequence
Set @ExecuteString ='Select @Sequence = Sequence From ' + @TableName + ' Where ID in ( Select Top 1 ID From ' + @TableName + ' Where Sequence >' + Cast( @itemSequence As Nvarchar(100) ) + ' Order by Sequence )'
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@Sequence float output, @itemSequence float ',
@TableName,@Sequence output,@itemSequence
-- add one to sequence
Set @ExecuteString ='Update ' + @TableName + ' Set Sequence = (@Sequence + 1) Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @Sequence float ',
@TableName,@ID ,@Sequence
return
End
Else if( @rowCount > 1 )
Begin
-- more than one item bigger than it
Set @ExecuteString ='Select @Sequence = Sum(Sequence) From ' + @TableName + ' Where ID in ( Select Top 2 ID From ' + @TableName + ' Where Sequence >' + Cast( @itemSequence As Nvarchar(100) ) + ' Order by Sequence )'
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@Sequence float output, @itemSequence float ',
@TableName,@Sequence output,@itemSequence
Set @itemSequence = @Sequence / 2
print @itemSequence
Set @ExecuteString ='Update ' + @TableName + ' Set Sequence = @itemSequence Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @itemSequence float ',
@TableName,@ID ,@itemSequence
return
End
Else
Begin
-- error
return
End
End
Else
-- move down
Begin
-- judge the item is the smallest
Set @ExecuteString = 'Select @rowCount = count(*) From ' + @TableName + ' Where Sequence <' + Cast( @itemSequence As Nvarchar(100) )
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@rowCount int output, @itemSequence float ',
@TableName,@rowCount output,@itemSequence
if( @rowCount = 0 )
Begin
-- it is the smallest
-- judge if existing more than one smallest
Set @ExecuteString = 'Select @rowCount = count(*) From ' + @TableName + ' Where Sequence =' + Cast( @itemSequence As Nvarchar(100) )
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@rowCount int output, @itemSequence float ',
@TableName,@rowCount output,@itemSequence
if( @rowCount > 1)
Begin
-- more than one the smallest ,to minus its sequence
Set @ExecuteString ='Update ' + @TableName + ' Set Sequence = (@itemSequence - 1) Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @itemSequence float ',
@TableName,@ID ,@itemSequence
return
End
Else
Begin
-- only one biggest
return
End
End
Else if( @rowCount = 1 )
Begin
-- only have one item smaller than it ,meaning it is the smaller
-- get the smallest's sequence
Set @ExecuteString ='Select @Sequence = Sequence From ' + @TableName + ' Where ID in ( Select Top 1 ID From ' + @TableName + ' Where Sequence <' + Cast( @itemSequence As Nvarchar(100) ) + ' Order by Sequence )'
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@Sequence float output, @itemSequence float ',
@TableName,@Sequence output,@itemSequence
-- minus one of its sequence
Set @ExecuteString ='Update ' + @TableName + ' Set Sequence = (@Sequence - 1) Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @Sequence float ',
@TableName,@ID ,@Sequence
return
End
Else if( @rowCount > 1 )
Begin
-- more than one item smaller than it
Set @ExecuteString ='Select @Sequence = Sum(Sequence) From ' + @TableName + ' Where ID in ( Select Top 2 ID From ' + @TableName + ' Where Sequence <' + Cast( @itemSequence As Nvarchar(100) ) + ' Order by Sequence )'
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@Sequence float output, @itemSequence float ',
@TableName,@Sequence output,@itemSequence
Set @itemSequence = @Sequence / 2
print @itemSequence
Set @ExecuteString ='Update ' + @TableName + ' Set Sequence = @itemSequence Where ID = ''' + Cast( @ID as nvarchar(50)) + ''''
Execute sp_executesql @ExecuteString ,N'@TableName nvarchar( 200 ),@ID uniqueidentifier, @itemSequence float ',
@TableName,@ID ,@itemSequence
return
End
Else
Begin
-- error
return
End
End