第一次任务为存储过程

USE [tanjiaping]
GO
/****** Object:  StoredProcedure [dbo].[pro_cutPoint]    Script Date: 03/02/2011 17:54:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[pro_cutPoint]
--要扣除的总分数
@sumPoint int,
--表UserPointRecord用到的数据
@UserID nvarchar(50),--用户ID
--表User_UsedPoint用到的数据
@RecordTypeId int,
@RecordType nvarchar(1000),
@OrderID int,
--表User_PointUseHistory用到的数据
@ActionID int,
@ActionName varchar(50)
as
--开启自动回滚事务
set xact_abort on
begin transaction cutTran

declare cursor_cutPoint scroll cursor
for
select PointRecord_Id,Point,UserPoint from UserPointRecord where Point>UserPoint and UserID=@UserID and CreateDate>DATEADD(MONTH,-18,GETDATE()) and IsValid=1 order by CreateDate asc--某用户的所有未用完积分的记录

declare @PointRecord_Id int;
declare @Point int;
declare @UserPoint int;

open cursor_cutPoint
fetch next from cursor_cutPoint into @PointRecord_Id,@Point,@UserPoint
while (@@FETCH_STATUS=0)
begin
 
  if(@sumPoint>(@Point-@UserPoint))--如果要扣除的分数大于当前记录剩余的分数
  begin
  set @sumPoint=@sumPoint-(@Point-@UserPoint)
  update UserPointRecord set UserPoint=@Point where PointRecord_Id=@PointRecord_Id
  if(@@error<>0)
  begin
   rollback transaction cutTran
  end
  insert into User_UsedPoint values(@RecordTypeId,@RecordType,@PointRecord_Id,@OrderID,-(@Point-@UserPoint),0,GETDATE(),1)
  if(@@error<>0)
  begin
   rollback transaction cutTran
  end
  insert into User_PointUseHistory values(@ActionID,@ActionName,-(@Point-@UserPoint),@UserID,GETDATE(),@UserID,GETDATE())
  if(@@error<>0)
  begin
   rollback transaction cutTran
  end
  end
  else if(@sumPoint<=(@Point-@UserPoint))
  begin
  update UserPointRecord set UserPoint=@sumPoint+@UserPoint where PointRecord_Id=@PointRecord_Id
  if(@@error<>0)
  begin
   rollback transaction cutTran
  end
  insert into User_UsedPoint values(@RecordTypeId,@RecordType,@PointRecord_Id,@OrderID,-@sumPoint,0,GETDATE(),1)
  if(@@error<>0)
  begin
   rollback transaction cutTran
  end
  insert into User_PointUseHistory values(@ActionID,@ActionName,-@sumPoint,@UserID,GETDATE(),@UserID,GETDATE())
  if(@@error<>0)
  begin
   rollback transaction cutTran
  end
  break
  end
  fetch next from cursor_cutPoint into @PointRecord_Id,@Point,@UserPoint
end
close cursor_cutPoint
deallocate cursor_cutPoint
commit transaction cutTran

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值