delimiter //
create procedure processorders()
begin
-- declare local variable
declare done boolean default 0;
declare o int;
declare t decimal(8,2);
-- declare the cursor
declare ordernumbers cursor for
select order_num from orders;
-- declare continue handler
declare continue handler for sqlstate '02000' set done=1; -- create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals (
    order_num INT,
    total DECIMAL(8 , 2 )
);
-- open the cursor
open ordernumbers;
-- loop through all rows
repeat
-- get order number
fetch ordernumbers into o;
-- get the total for this order
call ordertotal(o,1,t);
-- insert order and total into ordertotals
insert into ordertotals(order_num,total) values(o,t);
-- end of loop
until done end repeat;
-- close the cursor
close ordernumbers;
end//
delimiter //
create procedure ordertotal(
in onumber int,
in taxable boolean,
out ototal decimal(8,2)
) comment 'obtain order total , optionally adding tax'
begin
-- declare variable for total
declare total decimal(8,2);
-- declare tax percentage
declare taxrate int default 6; -- get the order total
SELECT 
    SUM(item_price * quantity)
FROM
    orderitems
WHERE
    order_num = onumber INTO total; -- is this taxable?
if taxable then
-- yes ,so add taxrate to the total
        select total+(total/100*taxrate) into total;
end if;
-- and finally, save to out variable
SELECT total INTO ototal;
end //
运行如下:
drop table ordertotals;
call processorders;
select * from ordertotals输出结果:
20005 158.86
20009 40.78
20006 58.30
20007 1060.00
20008 132.50
20008 132.50
请问为什么会在最后插入两个20008呀?这些代码就是《MYSQL必知必会》上面的,书上的结果就没有多插一条20008。谢谢