简单的学术项目。简单的程序。如果 Payments 表中已经存在某些内容 - 然后从该表中获取债务值,如果没有值 - 从 Tariff 表中获取它。但是为什么这样的条件不起作用呢?
ALTER PROCEDURE dbo.GetDebt
(
@LoanId int
)
AS
IF NOT EXISTS (SELECT top 1 * FROM Payment WHERE LoanId = @LoanId)
BEGIN
SELECT (TotalLoan + ( ( TotalLoan / 100 ) * Interest)) as Debt FROM Loan L, Tariff T
WHERE L.TariffIf = L.TariffId
END
ELSE
BEGIN
SELECT MIN(Debt) as Debt FROM Loan L
RIGHT OUTER JOIN Payment P -- Joins are cool.
ON L.LoanId = P.LoanId
WHERE P.LoanId = @LoanId
END