--同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)/**//*CREATETABLEAccount([Month][datetime]NOTNULLPrmaryKey,[PlanFare][money]NULL,[PutoutFare][money]NULL,[PlanPreFare][money]NULL,[PutoutPreFare][money]NULL)--Account表生成脚本*/selectidentity(int,1,1)as[id],*into#fromaccountorderby[month]ascCREATETABLE#Temp([id][int]NOTNULL,[Month][datetime]NOTNULL,[PlanFare][money]NULL,[PutoutFare][money]NULL,[PlanPreFare][money]NULL,[PutoutPreFare][money]NULL)declare@idintdeclaremcursorforselect[id]from#orderby[id]ascopenmfetchnextfromminto@idwhile(@@fetch_status=0)begininsertinto#Temp([id],[Month],PlanFare,PutoutFare,PlanPreFare,PutoutPreFare)select[id],[Month],PlanFare,PutoutFare,PlanPreFare,PutoutPreFarefrom#where[id]=@idUPDATE#SET#.PlanPreFare=(a.PlanFare-a.putoutFare)+(a.PlanPreFare-a.putoutPreFare)FROM#TempASaWHERE#.[id]=a.[id]+1--前行作依据,后行来修改(此次的后行,变成下次的前行,依此循环,直至表中的行遍历结束)truncatetable#Tempfetchnextfromminto@idendclosemdeallocatemUPDATEAccountSETAccount.PlanPreFare=a.PlanPreFareFROM#ASaWHEREAccount.[Month]=a.[Month]droptable#droptable#Temp