theoraclepl/sql goto statement is a sequential control structure available in oracle. the goto statement immediately transfers program control (called branching) unconditionally to a named statement label or block label. the statement or
the oracle pl/sql goto statement is a sequential control structure available in oracle. the goto statement immediately transfers program control (called branching) unconditionally to a named statement label or block label. the statement or label name must be unique in the block.
属于plsql控制语句,用于程序控制非条件跳至指定标签>。不易控制和维护,慎用!
二 例子:
1、简单goto 语句,判断数字是否为质数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
declare
p varchar2(30);
n pls_integer := 37; -- test any integer > 2 for prime
begin
for j in 2 .. round(sqrt(n)) loop
if n mod j = 0 then
-- test for prime
p := ' is not a prime number'; -- not a prime number
goto print_now;
end if;
end loop;
p := ' is a prime number';
>
dbms_output.put_line(to_char(n) || p);
end;
/
2、使用null避免报错:
1
2
3
4
5
6
7
8
9
10
11
12
declare
done boolean;
begin
for i in 1 .. 50 loop
if done then
goto end_loop;
end if;
> -- not allowed unless an executable statement follows
null; -- add null statement to avoid error
end loop; -- raises an error without the previous null
end;
/
3、使用goto分出一个环绕块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- example with goto statement
declare
v_last_name varchar2(25);
v_emp_id number(6) := 120;
begin
>
select last_name
into v_last_name
from employees
where employee_id = v_emp_id;
begin
dbms_output.put_line(v_last_name);
v_emp_id := v_emp_id + 5;
if v_emp_id
goto get_name; -- branch to enclosing block
end if;
end;
end;
/
----------------------