DB_51aspx/2014年/存储过程or函数/函数-查询会员对该优惠券使用的剩余次数.sql

(完整代码) 1KB

GO
-- =============================================
-- Author:李朴
-- Create date: 2014-3-29
-- Description:	函数-查询会员对该优惠券使用的剩余次数
-- =============================================
alter FUNCTION ufn_syTimesByTicket
(
 @uid int, ---用户id
 @ticketId int---优惠券id
)
RETURNS  int
AS
BEGIN
	 
	DECLARE @ticketTimes int  --优惠券总次数
	Declare @usedTimes int   --会员已经使用的次数
	declare @syTimes int    --剩余的次数,返回结果
	declare @sid int   --优惠券所在的店铺id
	declare @tmpUid int ---临时的会员id值
	set @ticketTimes=0
	set @usedTimes=0
	set @syTimes=0 
	set @sid=0
	set @tmpUid=0
 
	select   @ticketTimes=usedTimes,@sid=sid  from wx_ucard_ticket where id=@ticketId
	 --先判断该用户是否在该
     select  @tmpUid=id  from wx_ucard_users where id=@uid and sid=@sid
     
	if @tmpUid<=0
	begin
		set @syTimes=0
	end
	else
	begin
		select  @usedTimes=count(1) from wx_ucard_users_consumeinfo  where uid=@uid and moduleType='优惠券' and moduleActionId=@ticketId
		set @syTimes=@ticketTimes-@usedTimes
	end	
	RETURN @syTimes
	 
END
GO

-----test

--select * from wx_ucard_ticket;

 select  dbo.ufn_syTimesByTicket(23,2)

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读 AI

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。