Advertisement
Guest User

Untitled

a guest
Aug 8th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.33 KB | None | 0 0
  1. -- Declare the proper datatypes for these to match up with how they're
  2. -- defined in the database I'm just guessing on these
  3. DECLARE @AppID INT;
  4. DECLARE @Term INT;
  5. DECLARE @FinanaceAmount MONEY;
  6. DECLARE @IntRate DECIMAL(5, 3);
  7. -- Creates a way to grab data from the SELECT statement
  8. DECLARE Loan_Cursor CURSOR FOR
  9. SELECT AppID,
  10.   Term,
  11.   FinanceAmount,
  12.   IntRate
  13. FROM LOANTABLENAME;
  14. -- Start grabbing the data
  15. OPEN Loan_Cursor;
  16. FETCH NEXT FROM Loan_Cursor INTO @AppID, @Term, @FinanceAmount, @IntRate;
  17. -- Keep doing this until we can no longer grab anymore data
  18. WHILE @@FETCH_STATUS = 0
  19. BEGIN
  20.   -- Calcuate various loan variables and set our counter
  21.   DECLARE @I = 0;
  22.   DECLARE @OutstandingBalance = @FinanceAmount;
  23.   DECLARE @MonthlyAmount = @FinanceAmount / @Term;
  24.   DECLARE @InterestPaid = @MonthlyAmount * @IntRate;
  25.   WHILE @I < @TERM -- This should create @Term number of rows
  26.   BEGIN
  27.     -- I may have messed up on calucalting the outstanding balance
  28.     @OutstandingBalance = @OutstandingBalance - @MonthlyAmount;
  29.     INSERT INTO CUSTOMERPAYMENTTABLENAME (AppID, --Reference back to the app I assume is needed
  30.       IntrestPaid,
  31.       PrincipalPaid,
  32.       OutstandingBalance
  33.     VALUES (@AppID,
  34.       @IntrestPaid,
  35.       @MonthlyAmount - @IntrestPaid, --Prinicipal paid
  36.       @OustandingBalance
  37.     )
  38.   END
  39. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement