我就不信无法排序

lovingkiss 2004-10-14 06:28:03
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并非是最大或者最小值,而是啥都没有哇

邹建,测试我用你写的和我写的都会在极限的[最大或者最小的]测试下报错,咋办呢??
...全文
157 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Selectau_fname 2004-10-15
  • 打赏
  • 举报
回复
厉害!

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧