LN_REF_NO
是贷款的参考编号。LN_REF_NO
必须有一个 ACNO 和一个 LRAC。
它们都存储在 ACNO 列下的帐户中。
我将如何将它们分开?请看图片
ALTER PROCEDURE sp_FinalLoanPayment
AS
DECLARE @LN_REF_NO char(13)
DECLARE @TempA
TABLE (
[LN_REF_NO] [char](13),
[Status] [varchar](15),
[ACNO] [varchar](21),
[ACNOBalance] [dbo].[Amount]
)
DECLARE @TempL
TABLE (
[LN_REF_NO] [char](13),
[Status] [varchar](15),
[LRAC] [varchar](21),
[LRACBalance] [dbo].[Amount]
)
DECLARE @TempAll
TABLE (
[LN_REF_NO] [char](13),
[Status] [varchar](15),
[ACNO] [varchar](21),
[ACNOBalance] [dbo].[Amount],
[LRAC] [varchar](21),
[LRACBalance] [dbo].[Amount]
)
INSERT INTO @TempL (LN_REF_NO ,Status,LRAC,LRACBalance)
SELECT Loan.LN_REF_NO, Loan.Status,ACCOUNTS.ACNO ,ACCOUNTS.BALANCE
FROM Loan
INNER JOIN Accounts
ON LOAN.LRAC = ACCOUNTS.ACNO
WHERE Loan.Status ='Paid' AND ACCOUNTS.BALANCE>0
INSERT INTO @TempA (LN_REF_NO ,Status,ACNO,ACNOBalance)
SELECT Loan.LN_REF_NO, Loan.Status,ACCOUNTS.ACNO ,ACCOUNTS.BALANCE
FROM Loan
INNER JOIN Accounts
ON LOAN.ACNO = ACCOUNTS.ACNO
WHERE Loan.Status ='Paid' AND ACCOUNTS.BALANCE<0
-- START CURSOR 1
DECLARE @cursor CURSOR
SET @cursor = CURSOR FOR
select LN_REF_NO from @TempL
OPEN @cursor
FETCH NEXT
FROM @cursor INTO
@LN_REF_NO
WHILE (@@FETCH_STATUS = 0)
BEGIN
INSERT INTO @TempAll (LN_REF_NO ,Status,ACNO,ACNOBalance)
select LN_REF_NO,Status,ACNO,ACNOBalance
from @TempA
where LN_REF_NO = @LN_REF_NO
FETCH NEXT
FROM @cursor INTO
@LN_REF_NO
END
CLOSE @cursor
DEALLOCATE @cursor
-- CURSOR 1 END
select * from @TempAll order by LN_REF_NO
结果: