GOAL: to write a script that will update a field in one table with the sum of information from two other fields in a different table. I want to loop through this so I don't have to write 60 separate statements.
EXAMPLE: UPDATE CF_DD_IN SET CF_DD_IN.10 = tblCASHFLOW.I1 + tblCASHFLOW.D1
PROBLEM: I declare and set my source and destination variables because their values need to increment with every loop, but when I use the variables in the UPDATE statement SQL is wanting to set the value of my destination variable equal to my source variable. I need a way around this. Any help/advise is much appreciated!
CODE:
--Update CF_DD_IN table with info from tblCASHFLOW table
declare @count int
declare @month int
declare @year int
declare @sourcefield varchar(50)
declare @destinationfield varchar(50)
set @count = 1
set @month = 10
set @year = 2002
while @count <= 60
begin
set @sourcefield = 'tblCASHFLOW.I[' + cast(@count as varchar) + '] + tblCASHFLOW.D[' + cast(@count as varchar) + ']'
--Should result in 'tblCASHFLOW.I1 + tblCASHFLOW.D1' on the first pass
set @destinationfield = 'CF_DD_IN.[' + cast(@month as varchar) + ']'
--Should result in 'CF_DD_IN.10' on the first pass
UPDATE CF_DD_IN SET @destinationfield = @sourcefield <--- Herein lies the problem
FROM tblCASHFLOW INNER JOIN tblSORT ON tblCASHFLOW.SORTno = tblSORT.[SORT No:]
INNER JOIN CF_DD_IN ON tblSORT.[FCSC Asset NO:] = CF_DD_IN.ASSET_NO WHERE cf_dd_in.cf_yr = cast(@year as varchar)
PRINT 'Month: ' + cast(@count as varchar) + ' = ' + cast(@month as varchar) + ' ' + cast(@year as varchar) +
' : SET ' + @destinationfield + ' = ' + @sourcefield
set @count = @count + 1
set @month = @month + 1
if @month = 13 begin
set @month = 1
set @year = @year + 1
end
end
EXAMPLE: UPDATE CF_DD_IN SET CF_DD_IN.10 = tblCASHFLOW.I1 + tblCASHFLOW.D1
PROBLEM: I declare and set my source and destination variables because their values need to increment with every loop, but when I use the variables in the UPDATE statement SQL is wanting to set the value of my destination variable equal to my source variable. I need a way around this. Any help/advise is much appreciated!
CODE:
--Update CF_DD_IN table with info from tblCASHFLOW table
declare @count int
declare @month int
declare @year int
declare @sourcefield varchar(50)
declare @destinationfield varchar(50)
set @count = 1
set @month = 10
set @year = 2002
while @count <= 60
begin
set @sourcefield = 'tblCASHFLOW.I[' + cast(@count as varchar) + '] + tblCASHFLOW.D[' + cast(@count as varchar) + ']'
--Should result in 'tblCASHFLOW.I1 + tblCASHFLOW.D1' on the first pass
set @destinationfield = 'CF_DD_IN.[' + cast(@month as varchar) + ']'
--Should result in 'CF_DD_IN.10' on the first pass
UPDATE CF_DD_IN SET @destinationfield = @sourcefield <--- Herein lies the problem
FROM tblCASHFLOW INNER JOIN tblSORT ON tblCASHFLOW.SORTno = tblSORT.[SORT No:]
INNER JOIN CF_DD_IN ON tblSORT.[FCSC Asset NO:] = CF_DD_IN.ASSET_NO WHERE cf_dd_in.cf_yr = cast(@year as varchar)
PRINT 'Month: ' + cast(@count as varchar) + ' = ' + cast(@month as varchar) + ' ' + cast(@year as varchar) +
' : SET ' + @destinationfield + ' = ' + @sourcefield
set @count = @count + 1
set @month = @month + 1
if @month = 13 begin
set @month = 1
set @year = @year + 1
end
end