DECLARE @days INT -- 签到天数 SET @days=0 DECLARE @curr_day DATETIME -- 当前日期 SET @curr_day=DATEDIFF(DD,-1,GETDATE()) -- 默认昨天 DECLARE @todaySign BIT -- 今日是否签到 SET @todaySign=0 WHILE EXISTS(SELECT NULL FROM [Table_Sign] WHERE [UserID] = 1000120 AND DATEDIFF(DD,AddTime,@curr_day) = 1) -- 如果当前天的前一天有签到记录 BEGIN SET @days=@days+1 SET @curr_day=DATEDIFF(DD,-1,@curr_day) END IF((SELECT 1 FROM [Table_Sign] WHERE [UserID] = 1000120 AND CONVERT(varchar(10),[AddTime],23) = CONVERT(varchar(10),GETDATE(),23))IS NOT NULL) -- 今日是否完成签到 BEGIN SET @days=@days+1 SET @todaySign=1 END IF(@days>7) -- 是否为一个周期 BEGIN IF(@days%7=0) BEGIN SET @days=7 END ELSE BEGIN SET @days=@days%7 END END SELECT TOP 1 @days AS ContinueDays,@todaySign AS TodaySign FROM [Table_Sign] WHERE [UserID] = 1000120