我就不信无法排序
CREATE PROC MoveSortId
@SortId int, --要处理的id --功能:动态的调整表中的排序编号
@Arrow nvarchar(10) , --移动方向,'上'表示上移,'下'表示下移
@Table nvarchar(15) --相关表名
as
declare @s nvarchar(4000)
declare @nid int
if @SortId<1 return
set @s='
select @nid='+case when @Arrow='up' then 'min(SortId)' else 'max(SortId)' end
+'from ['+@Table+']'
exec sp_executesql @s,N'@nid int',@nid
print '---问题出在这里,返回来的@nid为空---'
print @nid
print '--------'
print @SortId
print '--------'
--如果不可移动,则退出
begin
if @nid=1
if @Arrow='up' return
else
begin
if @SortId=@nid and @Arrow<>'up' return
end
end
--取得移动后的新 SortId 值
set @nid=@SortId+case when @Arrow='up' then -1 else 1 end
if @nid < 1
begin
Set @nid = 1
end
--更新 SortId
exec ('update ['+@Table+'] set SortId=case when SortId=' + @SortId + ' then ' + @nid + ' else ' + @SortId + ' end
where SortId in(' + @SortId + ' , ' + @nid + ')')
GO
--=======================================================================
我改写成了这样,目前还有的问题,就是向上可能会无限增大,因为
exec sp_executesql @s,N'@nid int',@nid
这句话里面我不明白,所以返回来的@nid并非是最大或者最小值,而是啥都没有哇
邹建,测试我用你写的和我写的都会在极限的[最大或者最小的]测试下报错,咋办呢??