CREATE PROCEDURE dbo.BalanceEconomyGetAccountBalance @AHotelCustomerNumber INT, @ADateStamp CHAR(8), 
  @InhouseSaldo MONEY OUTPUT, @DueDepSaldo MONEY OUTPUT, @WantedDepSaldo MONEY OUTPUT AS
SET NOCOUNT ON

/*
  Inhouse, Due and wanted saldo
*/
DECLARE @NumberOfRuns TINYINT
DECLARE @SaldoForAccountType INT
DECLARE @AccountNumber INT

DECLARE @tmpSaldo MONEY

SET @NumberOfRuns = 3

WHILE @NumberOfRuns > 0
BEGIN
  IF @NumberOfRuns = 3
  BEGIN
    SET @InhouseSaldo = 0
    SET @SaldoForAccountType = 5
  END
  ELSE IF @NumberOfRuns = 2
  BEGIN
    SET @DueDepSaldo = 0
    SET @SaldoForAccountType = 8
  END
  ELSE IF @NumberOfRuns = 1
  BEGIN
    SET @WantedDepSaldo = 0
    SET @SaldoForAccountType = 9
  END

  DECLARE db_cur CURSOR FOR
    SELECT AccountNumber FROM Accounts WITH (NOLOCK)
    WHERE AccountType = @SaldoForAccountType
  OPEN db_cur

  FETCH NEXT FROM db_cur INTO @AccountNumber
  WHILE @@FETCH_STATUS = 0
  BEGIN
    SET @tmpSaldo = 0
    EXEC GetTransactionsSaldo @AccountNumber, @AHotelCustomerNumber, @tmpSaldo OUTPUT, @ADateStamp 
  
    IF @NumberOfRuns = 3
      SET @InhouseSaldo = @InhouseSaldo + ISNULL(@tmpSaldo, 0)
    ELSE IF @NumberOfRuns = 2
      SET @DueDepSaldo = @DueDepSaldo + ISNULL(@tmpSaldo, 0)
    ELSE IF @NumberOfRuns = 1
      SET @WantedDepSaldo = @WantedDepSaldo + ISNULL(@tmpSaldo, 0)
    FETCH NEXT FROM db_cur INTO @AccountNumber
  END

  CLOSE db_cur
  DEALLOCATE db_cur

  SET @NumberOfRuns = @NumberOfRuns - 1
END