select FMemId,FId,FCourseId,FItemId,FRemainPurTimes,FRemainFreeTimes,FCompId INTO #Course from TMemberCourse WHERE FExpiredDate < '2021-12-31' and fcourseid = 5518497342783127396 and (fremainpurtimes + FRemainFreeTimes) > 0 INSERT INTO TMemberCourseLog(FId,FMemId,FCardId,FPackId,FCourseId,FTime,FInPurTimes,FInFreeTimes,FOutPurTimes,FOutFreeTimes,FCompId,FBillType,FBizType,FBillId,FBillNo,FBillDetailId,FRecordTime) SELECT dbo.FNNewId(),FMemId,A.FId AS FCardId,0, ISNULL(FCourseId,0),getdate(),0,0,FRemainPurTimes,FRemainFreeTimes,FCompId,'QT','Clear',0,'后台清理',0,getdate() FROM #Course AS A UPDATE TMemberCourse SET FUsedPurTimes = FPurTimes,FUsedFreeTimes = FFreeTimes WHERE FId IN(SELECT FId FROM #Course)