create table mytable (fee1 money, fee2 money, fee3 money)
create table mytotals (feetotal money)
insert into mytable values (32, 11, 24)
insert into mytable values (12, 25, 44)
insert into mytable values (16, 14, 42)
I need to summarize totals for each column and insert them into another table, into a field of money datatype.
The problem is I need (or at least I think I need) to use dynamic SQL because the number of 'fee' fields could be changing over time.
Now there are 3 fields, tomorrow there could be 4 or 5.
declare @i int
declare @err int
declare @sql varchar(200)
set @i = 1
set @err = 0
while @err = 0
set @sql = 'select sum(fee' + cast(@i as varchar) + ')
insert mytotals values (@sql)
This code gives me an error trying to convert varchar (@sql) into money.
Thanks in advance!