参数的累加!
create table test1
--测试数据
(name varchar(10),
[201201] int,
[201202] int,
[201203] int,)
create table test2
(name varchar(10),
[201201] int,
[201202] int,
[201203] int,)
select * from dbo.test1
select * from dbo.test2
/*
name 201201 201202 201203
---------- ----------- ----------- -----------
Income 1 2 3
(1 row(s) affected)
name 201201 201202 201203
---------- ----------- ----------- -----------
income_tt 1 3 6
(1 row(s) affected)
*/
update text1 set 201202=(select SUM(201201+Mtd201202)
declare @str varchar(50)
declare @addstr varchar(200)
set @str=1
while @str<12
begin
set @addstr=.....
update test2 set @str=(select SUM(@addstr) from test1)
set @str=@str+1
........
好的,嘻嘻
[其他解释]
declare @i int
declare @str varchar(50)
declare @addstr varchar(200)
declare @sql varchar(500)
set @i=1
set @addstr = ''
while @i<4
begin
set @str ='m20120'+ convert(varchar(10),@i)
if @addstr = ''
set @addstr = 'm20120'+convert(varchar(10),@i)
else
set @addstr = @addstr + ' + ' + 'm20120'+convert(varchar(10),@i)
set @sql= ' update test2 set '+ @str+'=(select SUM('+@addstr+') from test1)'
print (@sql)
set @i=@i+1
end