I have a table with a credit and debit column.
I need to get the highest balance out of that, and I think a stored procedure is the way to do it, but I have no idea how.
I need to start with the first row, add the debits, subtract the credits and store the value A.
Second row is A+debit-credit=B; A = max(A,B) Repeat last step till the end.
Remember, I’m looking for the highest EVER, not the current, which would just be sum(debit-credit)
It seems to me that you’d want the running total to be
A+credit-debitbut switch them if needed.edit: In response to your comment about forming a stored function from this… unlike stored procedures, stored functions must return one value, so they can’t contain a
SELECTquery unless the query stores its result in a variable. This means the query must be guaranteed to have a single-value result. Below is a function I got to work, because in this case you only want the MAX value of@highanyway: