一对多的子表数据利用游标循环更新主表的数据
http://www.cnblogs.com/mcgrady/p/4182486.html#undefined
-- 声明变量
DECLARE
@doc_id AS varchar(50),
@finishdate AS datetime;
-- 声明游标
DECLARE C_Employees CURSOR FAST_FORWARD FOR
select wo.doc_id, max(ws.finishdate)as finishdate from f_workorder wo join f_wsection ws on wo.doc_id=ws.doc_id
where wo.status=3 group by wo.doc_id
OPEN C_Employees;
-- 取第一条记录
FETCH NEXT FROM C_Employees INTO @doc_id,@finishdate;
WHILE @@FETCH_STATUS=0
BEGIN
-- 操作
UPDATE f_workorder SET finishdate= @finishdate WHERE doc_id=@doc_id;
-- 取下一条记录
FETCH NEXT FROM C_Employees INTO @doc_id,@finishdate;
END
-- 关闭游标
CLOSE C_Employees;
-- 释放游标
DEALLOCATE C_Employees;