I’m not quite sure how to ask this, but let me ask by example:
I have a table beyond that I cannot change structure on. It records cash deposits at ATM-like terminals, and has one column for each denomination of monetary note. Right now when I need the total value of a deposit I need code like this:
(rd.Count_R10 * 10) + (rd.Count_R20 * 20) + (rd.Count_R50 * 50) + (rd.Count_R100 * 100) + (rd.Count_R200 * 200)
I would like to write a T-SQL function that gives me that total value, but for any row, not for an entire query, so my function would be something like:
CREATE FUNCTION DepositTotal
(
@row ????
)
RETURNS money
AS
BEGIN
RETURN (row.Count_R10 * 10) + (row.Count_R20 * 20) + (row.Count_R50 * 50) + (row.Count_R100 * 100) + (row.Count_R200 * 200)
END
Then I would call it something like:
select
DepositDate
, DepositTotal(thisRow)
, BatchId
from Deposits
Does the solution have to be a Function? Or is the limiting factor that you can’t alter the structure of the table? You could write a view over the table that adds a column with your total value..
Then just select from
DepositsWithTotalinstead ofDeposits