CREATE TABLE AREAS
(
RADIUS NUMBER(5),
AREA NUMBER(14,2)
);
方法一:使用 goto
DECLARE
pi CONSTANT NUMBER (9, 7) := 3.1415927;
radius INTEGER (5);
area NUMBER (14, 2);
BEGIN
radius := 0;
LOOP
<<here>>
radius := radius + 1;
IF radius = 4
THEN
GOTO here;
ELSE
area := pi * POWER (radius, 2);
INSERT INTO areas
VALUES (radius, area);
EXIT WHEN area > 100;
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;
-- 方法二:使用 exception
-- 利用系統 exception
DECLARE
pi CONSTANT NUMBER (9, 7) := 3.1415927;
radius INTEGER (5);
area NUMBER (14, 2);
some_variable NUMBER (14, 2);
BEGIN
radius := 0;
LOOP
BEGIN
radius := radius + 1;
some_variable := 1 / (radius - 4);
area := pi * POWER (radius, 2);
INSERT INTO areas
VALUES (radius, area);
EXIT WHEN area > 100;
EXCEPTION
WHEN ZERO_DIVIDE
THEN
NULL;
END;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;