Using Sol's code example you could use ISNULL, that may be more efficent than an IF or CASE statement
Code:
CREATE PROCEDURE GetCurrentBalance
@AccountId int,
@CurrentBalance decimal
AS
DECLARE @Adjustments decimal,
@LegalFees decimal,
@Receipts decimal,
@OpeningBalance decimal
SELECT @Adjustments = SUM(Amounts) FROM Adjustments WHERE AccountID = @AccountID
SELECT @LegalFees = SUM(Amounts) FROM LegalFees WHERE AccountID = @AccountID
SELECT @Receipts = SUM(Amounts) FROM Receipts WHERE AccountID = @AccountID
SELECT @OpeningBalance = OpenBal FROM Accounts WHERE AccountId = @AccountID
SELECT @CurrentBalance = ISNULL(@Adjustments, 0) + ISNULL(@LegalFees, 0) + ISNULL(@OpeningBalance, 0) - ISNULL(@Receipts, 0)
Return @CurrentBalance
GO
Bookmarks