在 SQL 中自动生成每月数据

问题描述

我是 ASP.NET 和 MSsql 的新手,我遇到了一个问题,我有一个房租支付系统,我想在每个月的月初自动生成一个Pay-Rent 这样的提醒我已经有房客在当月第一天后给房东的付房租和退还预付款的交易

...目前我的数据库显示这个

EntryDate   Transaction                Advance     Rent Paid  Description         Advanced  
                                       Return
2021-01-27  Rent Paid                  0.00        2000.00   2k rent received     5000.00   
2021-02-20  Rent Paid                  0.00        9000.00   9000 rent paid       5000.00   
2021-03-10  Rent Paid                  0.00        100.00    100 rent paid        5000.00   
2021-04-6   Rent Paid                  0.00        99.00     99 amount paid       5000.00   
2021-05-2   Advanced Money Returned    1000.00     0.00      1000 rent returned   5000.00   

...我想做的是这个

EntryDate   Transaction               Rent     Advance     RentPaid     Description         Advanced  
                                               Return
2021-01-01  Please Pay your rent      5000      0              0           0                  0
2021-01-27  Rent Paid                  0      0.00             5000.00     5k rent received   5000.00   
2021-02-01  Please Pay your rent      5000      0              0           0                  0
2021-02-20  Rent Paid                  0      0.00             5000.00     5k rent paid       5000.00   
2021-03-01  Please Pay your rent      5000      0              0           0                  0
2021-03-10  Rent Paid                  0      0.00             5000.00     5k rent paid       5000.00   
2021-04-01  Please Pay your rent      5000      0              0           0                  0
2021-04-6   Rent Paid                  0      0.00             5000.00     5k amount paid     5000.00   
2021-05-01  Please Pay your rent      5000      0              0           0                  0
2021-05-2   Advanced Money Returned    0      1000.00          5000.00     5k rent returned   5000.00   

我想要的这一行是在每个月初自动生成

2021-01-01  Please Pay your rent      5000      0              0           0                  0

这是我的 StoredProcedure 的代码



        
SELECT RR.EntryDate,(N'RentPaid') as TransactionName,0 as AdvanceReturn,RR.AmountPaid as RentPaid,RR.Description,MemberStartAmount as Advanced
        FROM tblRentReceive as RR WHERE UserId = @UserId 
        UNION ALL   
        SELECT OGF.EntryDate,(N'Advanced Money Returned') as TransactionName,AR.Amount as Amount,0 as RentReceive,AR.Description,@MemberStartAmount as Advanced
        FROM tblAdvancedReturn as AR WHERE AR.UserId = @UserId and AR.TenantId=@TenantId 
    ORDER BY EntryDate ASC 
END
END


我需要在哪里添加自动每月行而不破坏两个表之间的联合

解决方法

伪代码:

START    
for each record in master-record
begin
      get the %MONTH% and %YEAR% of the record
      create new record with day:1,month=%MONTH%,and year=%YEAR%,Transaction='Please pay your rent',and etc.
      add the record and calculate the balance
end
add final-record 'Advanced Money Record' with the calculated balance

END

如果你有想法,你可以把它翻译成 tsql-stored-proc。