LN_REF_NO
是贷款的参考编号。 LN_REF_NO
必须有一个ACNO和一个LRAC。
他们两人都在ACNO专栏下存入账户。
我将如何分开他们? PLS看图像
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
结果:
答案 0 :(得分:1)
我认为您应该能够在需要时使用此查询获取此数据:
SELECT
ln.LN_REF_NO, ln.Status, a1.ACNO, a1.BALANCE, a2.ACNO, a2.BALANCE
FROM
dbo.Loan ln
INNER JOIN
dbo.Accounts a1 ON ln.LRAC = a1.ACNO
INNER JOIN
dbo.Accounts a2 ON ln.ACNO = a2.ACNO
WHERE
ln.Status = 'Paid'
AND a1.Balance > 0
AND a2.Balance > 0
您基本上只需加入Accounts
表两次 - 一次是指向Loan.LRAC
的链接,另一次指向Loan.ACNO
上的链接。
基于这两个连接,您只需从所需的所有连接表中选择那些列 - 这就是真正的全部!没有光标,没有临时表 - 没有必要的东西!