第一次任务为存储过程
2011-03-02 17:57
309 查看
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
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
相关文章推荐
- 第一次写MySQL存储过程遇到的关于DELIMITER的问题
- [置顶] Oracle job procedure 存储过程定时任务
- 一个mysql的存储过程 第一次接触请大家指点一下
- mysql启用系统定时任务自动调用存储过程动态拼接表
- mysql 计划任务 调用存储过程
- oralce定时执行存储过程任务设置步骤详细
- mysql定时任务与存储过程实例
- 创建数据库存储过程每天计划任务更新前台数据字段是否到期提醒
- Oracle 的存储过程和定时任务
- Oracle job procedure 存储过程定时任务
- 第一次写的存储过程
- 记录我第一次写sql server 2005的存储过程
- ORACLE定时任务调用存储过程动态为表添加分区
- ORACLE 11G在存储过程里面遍历游标, 调用job任务定时执行
- Oracle job procedure 存储过程定时任务
- [转]Oracle job procedure 存储过程定时任务
- MySQL存储过程与定时任务-介绍与实践
- 存储过程及任务的创建
- oracle创建存储过程定时任务时报错的问题
- 添加表、字段、存储过程、定时任务