如何高效迭代 --> 测试数据:[kim] if object_id('[kim]') is not null drop table [kim] go create table kim(stockcode varchar(8),stockdate datetime,closeprice decimal(18, 3), xxx decimal(18, 3)) insert [kim] select 'SH601318','20130701',34.3,NULL union all select 'SH601318','20130703',33.81,NULL union all select 'SH601318','20130702',34.22,NULL union all select 'SZ002322','20130808',14.60,NULL union all select 'SH601318','20130706',34.03,NULL union all select 'SZ002322','20130809',14.16,NULL
create table kim(stockcode varchar(8),stockdate datetime,closeprice decimal(18, 3), xxx decimal(18, 3)) insert [kim] select 'SH601318','20130701',34.3,NULL union all select 'SH601318','20130703',33.81,NULL union all select 'SH601318','20130702',34.22,NULL union all select 'SZ002322','20130808',14.64,NULL union all select 'SH601318','20130706',34.03,NULL union all select 'SZ002322','20130809',14.16,NULL
;with t1 as ( select row=row_number() over(partition by stockcode order by stockdate), stockcode, stockdate, closeprice from kim ), t2 as ( select row, stockcode, stockdate, closeprice, xxx=closeprice from t1 where row=1 union all select t1.row, t1.stockcode, t1.stockdate, t1.closeprice, cast((t2.xxx*25+t1.closeprice*2)/27 as decimal(18,3)) from t1, t2 where t1.stockcode=t2.stockcode and t1.row=t2.row+1 ) update kim set xxx=t2.xxx from kim join t2 on kim.stockcode=t2.stockcode and kim.stockdate=t2.stockdate option(MAXRECURSION 0);