SQl 存储过程完成记录的上下移动操作
.
分类: SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[sortGroup]
@groupid int,
@up bit,
@createuser int
as
declare @dqsort int
declare @shyi int
declare @syid int
declare @xyid int
select @dqsort=Sort from T_Group where Group_Id=@groupid and create_user=@createuser --取出其序号
if @up = 1
begin
select top 1 @syid=Group_Id,@shyi=Sort from T_Group where Sort<@dqsort and create_user=@createuser order by Sort desc --取出其上一条Id来
end
else
begin
select top 1 @syid=Group_Id,@shyi=Sort from T_Group where Sort > @dqsort and create_user=@createuser order by Sort asc
end
begin tran
update T_Group set sort=@dqsort where Group_Id=@syid --把上一条的sort高为当前的条的Sort
update T_Group set Sort=@shyi where Group_id = @groupid
if @@ERROR = 0
COMMIT TRAN
ELSE
ROLLBACK TRAN
GO
ALTER TABLE [dbo].[T_City] WITH CHECK ADD FOREIGN KEY([proID])
REFERENCES [dbo].[T_Province] ([proID])
GO