insert into table(人员编号,年度,期初,期末) select 人员编号,2007,期末,0 from table where 年度=2006
update newtable set newtable.期初 =oldtable.期末 from 表 as newtable ,表 as oldtable where newtable.人员编号 = oldTable.人员编号 and newtable.年度 =datepart(year,getdate()) and oldtable.年度 = (datepart(year,getdate())-1)
--trycreate table T(人员编号 char(1), 年度 int, 期初 int, 期末 int) insert T select 'A', 2006, 100, 200 insert T select 'B', 2006, 120, 300 insert T select 'A', 2005, 80, 100insert T select 人员编号, 2007, 期末, NULL from T where 年度=2006select * from T order by 年度, 人员编号--result 人员编号 年度 期初 期末 ---- ----------- ----------- ----------- A 2005 80 100 A 2006 100 200 B 2006 120 300 A 2007 200 NULL B 2007 300 NULL(5 row(s) affected)
set newtable.期初 =oldtable.期末
from 表 as newtable ,表 as oldtable
where newtable.人员编号 = oldTable.人员编号
and newtable.年度 =datepart(year,getdate())
and oldtable.年度 = (datepart(year,getdate())-1)
insert T select 'A', 2006, 100, 200
insert T select 'B', 2006, 120, 300
insert T select 'A', 2005, 80, 100insert T select 人员编号, 2007, 期末, NULL from T where 年度=2006select * from T order by 年度, 人员编号--result
人员编号 年度 期初 期末
---- ----------- ----------- -----------
A 2005 80 100
A 2006 100 200
B 2006 120 300
A 2007 200 NULL
B 2007 300 NULL(5 row(s) affected)