基于嵌套游标的更新
Nested Cursor based update
这是针对 SSRS 报告的,所以我使用的是临时 tables。我有两个 table,一个用于交易,我只是用它来提取帐户和金额。第二个是摊销拖欠信息,我正在尝试根据交易进行调整,但我 运行 陷入语法问题。 case 语句是否不允许与游标或更新一起使用?
--Example Transaction:Account 123456 Principal 500.00 Interest 250.00
delinquent 5 months of 200 principal each month, the transaction had principal amount of 500, the first two are left alone, the third is reduced to 100 to match the total to the transaction amount, leaving a remaining amount of 0
--Delinquent data
-- 11/2015 Prin 200 Int 80 -> Prin 0 Int 0 (running total Prin 500 Int 250)
-- 10/2015 Prin 200 Int 80 -> Prin 0 Int 10 (running total Prin 500 Int 250)
-- 9/2015 Prin 200 Int 80 -> Prin 100 Int 80 (running total Prin 500 Int 240)
-- 8/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 400 Int 160)
-- 7/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 200 Int 80) <- process starts from the oldest and goes up.
这需要使用游标,因为我无法在一个集合中处理,因为它必须从最旧的行开始,调整,找到剩余的总数,然后继续调整下一行。如果达到交易金额,则将剩余的行清零。然后我将剩余金额退还给交易 table。
Declare TranCursor Cursor FORWARD_ONLY
For Select LoanNumber, PrincipalCollected, InterestCollected, ServiceFee, PayoffPrincipal,PayoffInterest,PayoffServiceFee
From #transFinal
FOR UPDATE OF PayoffPrincipal,PayoffInterest,PayoffServiceFee
Open TranCursor;
Fetch next from TranCursor into @TranLoan, @TranPrin, @TranInt, @TranServ, @POPrin,@POInt,@POServ
while (@@Fetch_status = 0)
Begin
--Process this individual loan's transaction by going through each set of amortized amounts starting with the oldest and reducing excess of transaction amounts to zero.
--eg. delinquent 3 months of 200 principal each month, the transaction had principal amount of 500, the first two are left alone, the third is reduced to 100 to match the total to the transaction amount
--Transaction Principal 500 Interest 250
--Delinquent data
-- 11/2015 Prin 200 Int 80 -> Prin 0 Int 0 (running total Prin 500 Int 250)
-- 10/2015 Prin 200 Int 80 -> Prin 0 Int 10 (running total Prin 500 Int 250)
-- 9/2015 Prin 200 Int 80 -> Prin 100 Int 80 (running total Prin 500 Int 240)
-- 8/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 400 Int 160)
-- 7/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 200 Int 80) <- process starts from the oldest and goes up.
Declare DelqCursor CURSOR FORWARD_ONLY
FOR select LoanNumber,DelqPrin ,DelqInt ,DelqServFee from #dq
where LoanNumber = @TranLoan
Order by PaidToDate Asc
For update OF DelqPrin, DelqInt, DelqServFee;
Open DelqCursor;
-----------------------------------------------------------------------------------------------------------
-- Processing individual row of delinquent data
-----------------------------------------------------------------------------------------------------------
Fetch next from DelqCursor into @DelqPrin, @DelqInt, @DelqServ, @DelqPTD --, @POPrin,@POInt,@POServ
while (@@Fetch_status = 0)
BEGIN
CASE when @TranPrin = 0 then set @DelqPrin = 0 -- Syntax error on case
WHEN @TranPrin >0 and @TranPrin > @DelqPrin then -- Syntax error on when
set @TranPrin = @TranPrin - @DelqPrin
set @ColPrin = @ColPrin + @DelqPrin
WHEN @TranPrin >0 and @TranPrin < @DelqPrin then
set @ColPrin = @ColPrin + @TranPrin
set @TranPrin = 0
set @DelqPrin = @DelqPrin -@TranPrin
end
CASE when @TranInt = 0 then set @DelqInt = 0
WHEN @TranInt >0 and @TranInt > @DelqInt then
set @TranInt = @TranInt - @DelqInt
set @ColInt = ColInt + @DelqInt
WHEN @TranInt >0 and @TranInt < @DelqInt then
set @ColInt = @ColInt + @TranInt
set @TranInt = 0
set @DelqInt = @DelqInt -@TranInt
end
CASE when @TranServ = 0 then set @DelqServFee = 0
WHEN @TranServ >0 and @TranServ> @DelqServ then
set @TranServ = @TranServ - @DelqServ
set @ColServ = ColServ + @DelqServ
WHEN @TranServ >0 and @TranServ < @DelqServ then
set @ColServ = @ColServ + @TranServ
set @TranServ = 0
set @DelqServ = @DelqServ -@TranServ
end
Fetch next from DelqCursor into @DelqPrin, @DelqInt, @DelqServ, @DelqPTD
End
-----------------------------------------------------------------------------------
--All rows of delinquent data for a single loan have been processed. Now we update the Payoff columns
----------------------------------------------------------------------------------
Set @PoPrin = @ColPrin
Set @PoInt = @ColInt
Set @PoServ = @ColServ
--Todo Finish update statement for outside loop to update transaction table
Close DelqCursor-- Finished with delinquent data for this loan. We close the cursor
Fetch next from TranCursor into @TranLoan, @TranPrin, @TranInt, @TranServ, @POPrin,@POInt,@POServ --Start Processing next loan
End
Close TranCursor
deallocate DelqCursor
deallocate TranCursor
我将不胜感激我能得到的任何见解,以弄清楚为什么我的 case when 语句给我错误。我在 msdn 语法中看不到任何会阻止案例逻辑工作的内容。
您编写 CASE
语句的方式将不起作用。案例逻辑需要在 SELECT
或 SET
内发生。你的 在外面 ,SET
在中间,在 THEN
之后。
对于代码中的每个 CASE
,您都需要将逻辑更改为类似的内容。
IF @TranPrin = 0
SET @DelqPrin = 0
IF @TranPrin >0 AND @TranPrin > @DelqPrin
BEGIN
SET @TranPrin = @TranPrin - @DelqPrin
SET @ColPrin = @ColPrin + @DelqPrin
END
IF @TranPrin > 0 AND @TranPrin < @DelqPrin
BEGIN
SET @ColPrin = @ColPrin + @TranPrin
SET @TranPrin = 0
SET @DelqPrin = @DelqPrin -@TranPrin
END
这是针对 SSRS 报告的,所以我使用的是临时 tables。我有两个 table,一个用于交易,我只是用它来提取帐户和金额。第二个是摊销拖欠信息,我正在尝试根据交易进行调整,但我 运行 陷入语法问题。 case 语句是否不允许与游标或更新一起使用?
--Example Transaction:Account 123456 Principal 500.00 Interest 250.00
delinquent 5 months of 200 principal each month, the transaction had principal amount of 500, the first two are left alone, the third is reduced to 100 to match the total to the transaction amount, leaving a remaining amount of 0
--Delinquent data
-- 11/2015 Prin 200 Int 80 -> Prin 0 Int 0 (running total Prin 500 Int 250)
-- 10/2015 Prin 200 Int 80 -> Prin 0 Int 10 (running total Prin 500 Int 250)
-- 9/2015 Prin 200 Int 80 -> Prin 100 Int 80 (running total Prin 500 Int 240)
-- 8/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 400 Int 160)
-- 7/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 200 Int 80) <- process starts from the oldest and goes up.
这需要使用游标,因为我无法在一个集合中处理,因为它必须从最旧的行开始,调整,找到剩余的总数,然后继续调整下一行。如果达到交易金额,则将剩余的行清零。然后我将剩余金额退还给交易 table。
Declare TranCursor Cursor FORWARD_ONLY
For Select LoanNumber, PrincipalCollected, InterestCollected, ServiceFee, PayoffPrincipal,PayoffInterest,PayoffServiceFee
From #transFinal
FOR UPDATE OF PayoffPrincipal,PayoffInterest,PayoffServiceFee
Open TranCursor;
Fetch next from TranCursor into @TranLoan, @TranPrin, @TranInt, @TranServ, @POPrin,@POInt,@POServ
while (@@Fetch_status = 0)
Begin
--Process this individual loan's transaction by going through each set of amortized amounts starting with the oldest and reducing excess of transaction amounts to zero.
--eg. delinquent 3 months of 200 principal each month, the transaction had principal amount of 500, the first two are left alone, the third is reduced to 100 to match the total to the transaction amount
--Transaction Principal 500 Interest 250
--Delinquent data
-- 11/2015 Prin 200 Int 80 -> Prin 0 Int 0 (running total Prin 500 Int 250)
-- 10/2015 Prin 200 Int 80 -> Prin 0 Int 10 (running total Prin 500 Int 250)
-- 9/2015 Prin 200 Int 80 -> Prin 100 Int 80 (running total Prin 500 Int 240)
-- 8/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 400 Int 160)
-- 7/2015 Prin 200 Int 80 -> Prin 200 Int 80 (running total Prin 200 Int 80) <- process starts from the oldest and goes up.
Declare DelqCursor CURSOR FORWARD_ONLY
FOR select LoanNumber,DelqPrin ,DelqInt ,DelqServFee from #dq
where LoanNumber = @TranLoan
Order by PaidToDate Asc
For update OF DelqPrin, DelqInt, DelqServFee;
Open DelqCursor;
-----------------------------------------------------------------------------------------------------------
-- Processing individual row of delinquent data
-----------------------------------------------------------------------------------------------------------
Fetch next from DelqCursor into @DelqPrin, @DelqInt, @DelqServ, @DelqPTD --, @POPrin,@POInt,@POServ
while (@@Fetch_status = 0)
BEGIN
CASE when @TranPrin = 0 then set @DelqPrin = 0 -- Syntax error on case
WHEN @TranPrin >0 and @TranPrin > @DelqPrin then -- Syntax error on when
set @TranPrin = @TranPrin - @DelqPrin
set @ColPrin = @ColPrin + @DelqPrin
WHEN @TranPrin >0 and @TranPrin < @DelqPrin then
set @ColPrin = @ColPrin + @TranPrin
set @TranPrin = 0
set @DelqPrin = @DelqPrin -@TranPrin
end
CASE when @TranInt = 0 then set @DelqInt = 0
WHEN @TranInt >0 and @TranInt > @DelqInt then
set @TranInt = @TranInt - @DelqInt
set @ColInt = ColInt + @DelqInt
WHEN @TranInt >0 and @TranInt < @DelqInt then
set @ColInt = @ColInt + @TranInt
set @TranInt = 0
set @DelqInt = @DelqInt -@TranInt
end
CASE when @TranServ = 0 then set @DelqServFee = 0
WHEN @TranServ >0 and @TranServ> @DelqServ then
set @TranServ = @TranServ - @DelqServ
set @ColServ = ColServ + @DelqServ
WHEN @TranServ >0 and @TranServ < @DelqServ then
set @ColServ = @ColServ + @TranServ
set @TranServ = 0
set @DelqServ = @DelqServ -@TranServ
end
Fetch next from DelqCursor into @DelqPrin, @DelqInt, @DelqServ, @DelqPTD
End
-----------------------------------------------------------------------------------
--All rows of delinquent data for a single loan have been processed. Now we update the Payoff columns
----------------------------------------------------------------------------------
Set @PoPrin = @ColPrin
Set @PoInt = @ColInt
Set @PoServ = @ColServ
--Todo Finish update statement for outside loop to update transaction table
Close DelqCursor-- Finished with delinquent data for this loan. We close the cursor
Fetch next from TranCursor into @TranLoan, @TranPrin, @TranInt, @TranServ, @POPrin,@POInt,@POServ --Start Processing next loan
End
Close TranCursor
deallocate DelqCursor
deallocate TranCursor
我将不胜感激我能得到的任何见解,以弄清楚为什么我的 case when 语句给我错误。我在 msdn 语法中看不到任何会阻止案例逻辑工作的内容。
您编写 CASE
语句的方式将不起作用。案例逻辑需要在 SELECT
或 SET
内发生。你的 在外面 ,SET
在中间,在 THEN
之后。
对于代码中的每个 CASE
,您都需要将逻辑更改为类似的内容。
IF @TranPrin = 0
SET @DelqPrin = 0
IF @TranPrin >0 AND @TranPrin > @DelqPrin
BEGIN
SET @TranPrin = @TranPrin - @DelqPrin
SET @ColPrin = @ColPrin + @DelqPrin
END
IF @TranPrin > 0 AND @TranPrin < @DelqPrin
BEGIN
SET @ColPrin = @ColPrin + @TranPrin
SET @TranPrin = 0
SET @DelqPrin = @DelqPrin -@TranPrin
END