use wuhan_kelin SELECT * FROM TConsumeBill ORDER BY FBillTime desc DECLARE List FOR SELECT SELECT A.FMemId,A.FCardId,A.FAcctId,A.FBalance,B.FTypeId AS FCardTypeId,B.FCompId INTO #Acct FROM TMemberCardAccount AS A,TMemberCard AS B WHERE FBalance > 0 AND A.FCardId = B.FCardId AND B.FCreateDate < '2021-10-01' AND A.FAcctId = 3 AND B.FDeleted = 0 drop table #newacct SELECT A.FMemId,A.FCardId,A.FCardTypeId,A.FCompId,A.FAcctId,A.FOldBalance ,B.FNewOutBalance,3 AS FOutAcctId , 4877729984662584702 AS FInAcctId, A.FOldBalance - ISNULL(B.FNewOutBalance,0) AS FChangeBalance ,--应该转入老账户的余额 dbo.FNNewId() AS FTransId INTO #NewAcct FROM ( --获取截至到10月1号的余额 SELECT A.FMemId,A.FCardId,B.FCardTypeId,B.FCompId,A.FAcctId,SUM(FInAmount) - SUM(FOutAmount) AS FOldBalance FROM TMemberCardAcctHist AS A,#Acct AS B WHERE A.FCardId = B.FCardId AND A.FAcctId = B.FAcctId AND A.FTime < '2021-10-01' GROUP BY A.FMemId,A.FCardId,A.FAcctId ,B.FCompId,B.FCardTypeId ) AS A LEFT JOIN ( --获取10月1号之后消费的金额 SELECT A.FCardId,A.FAcctId, SUM(FOutAmount) AS FNewOutBalance FROM TMemberCardAcctHist AS A,#Acct AS B WHERE A.FCardId = B.FCardId AND A.FAcctId = B.FAcctId AND A.FTime > '2021-10-01' GROUP BY A.FCardId,A.FAcctId ) AS B ON A.FCardId = B.FCardId AND A.FAcctId = B.FAcctId WHERE A.FOldBalance > ISNULL(B.FNewOutBalance,0) BEGIN TRAN INSERT TMemberCardAcctHist(FId,FMemId,FCardId,FAcctId,FTime,FCardTypeId,FCompId,FBillType,FBizType,FTransId,FTransNo,FInAmount,FOutAmount,FBalance,FArrear,FRecordTime,FMemo) select dbo.FNNewId(),A.FMemId,A.FCardId,A.FInAcctId,getdate(),A.FCardTypeId,A.FCompId,'ZZ','Transfer.In',A.FTransId,'',A.FChangeBalance,0,0,0,getdate(),'2021-10-01之前的储值账户余额转入到老储值账户' FROM #NewAcct AS A INSERT TMemberCardAcctHist(FId,FMemId,FCardId,FAcctId,FTime,FCardTypeId,FCompId,FBillType,FBizType,FTransId,FTransNo,FInAmount,FOutAmount,FBalance,FArrear,FRecordTime,FMemo) select dbo.FNNewId(),A.FMemId,A.FCardId,A.FOutAcctId,getdate(),A.FCardTypeId,A.FCompId,'ZZ','Transfer.Out',A.FTransId,'',0,A.FChangeBalance,0,0,getdate(),'2021-10-01之前的储值账户余额转入到老储值账户' FROM #NewAcct AS A INSERT TMemberCardAccount(FId,FMemId,FCardId,FAcctId,FDeposit,FBalance,FArrear,FCreateDate,FMemo) SELECT dbo.FNNewId(),A.FMemId,A.FCardID,A.FInAcctId,A.FChangeBalance,A.FChangeBalance,0,GETDATE(),'' from #NewAcct AS A sp_rename '#NewAcct','TChangeAccount_2021_10_01' select * into TChangeAccount_2021_10_01 FROM #NewAcct UPDATE A SET A.FBalance = A.FBalance - B.FChangeBalance FROM TMemberCardAccount AS A,#NewAcct AS B WHERE A.FCardId = B.FCardId AND A.FAcctId = B.FAcctId ROLLBACK TRAN SELECT * FROM ( SELECT FCardId,FAcctId,FBalance FROM TMemberCardAccount ) AS T1, ( SELECT FCardId,FAcctId, SUM(FInAmount) - SUM(FOutAmount) AS FBalance FROM TMemberCardAcctHist GROUP BY FCardId,FAcctId ) AS T2 WHERE T1.FCardId = T2.FCardId AND T1.FAcctId = T2.FAcctId AND T2.FBalance != T1.FBalance SELECT 'EXEC dbo.PRecalcCardAccHistBalance ' + CAST(FCardId AS VARCHAR) FROM TMemberCard WHERE FCardId IN(SELECT FCardId FROM #Acct) SELECT * FROM TPayAccount SELECT * FROM #Acct AS A,#hist AS B WHERE A.FCardId = B.FCardId AND A.FAcctId = B.FAcctId AND B.FOldBalance != A.FBalance SELECT 'EXEC PDeleteMemberSaleBill '+ CAST(FBilLid AS VARCHAR) FROM TMemberSaleBill WHERE FBilLTime > '2022-01-01' AND FBilLTime <= '2022-01-02' ORDER BY FBilLTime desc SELECT C.FNo,C.FAbbr,A.FBillNo FROM TMemberSaleBill AS A,TCompany AS C WHERE FBilLTime > '2022-01-01' AND FBilLTime <= '2022-01-02' AND C.FId = A.FCompId ORDER BY C.FNo, FBilLTime desc