create or replace procedure order_Pro(c out number) is
--变量
v_cid varchar2(32);
--游标
cursor cur_book is select bookid,max(ord) o,sum(num) s from t_temp_chapter group by bookid;
begin
--第一步:将统计数据加入到图书表
--循环游标
for book_item in cur_book loop
--查询出一本书最后的章节编号
select cid into v_cid from t_temp_chapter where bookid=book_item.bookid and ord=book_item.o;
--将最后的章节编号和总字数保存在图书表中
update t_book set allnum=allnum+book_item.s,lastCid=v_cid where bookid=book_item.bookid;
end loop;
--第二步:将临时章节表中的数据,导入到正式章节表中,并且加入两个字段
insert into t_chapter(cid,bookid,num,cname,ord,free,createTime)
(select cid,bookid,num,cname,ord,
(select free from t_book where bookid=t.bookid),Sysdate from t_temp_chapter t);
commit;
--第三步:统计返回的数据
select count(1) into c from t_temp_chapter;
--第四步:删除临时章节表中的数据
delete from t_temp_chapter;
commit;
end book_Pro;