当前位置: 代码迷 >> SQL >> PLSQL流程统制
  详细解决方案

PLSQL流程统制

热度:67   发布时间:2016-05-05 14:49:38.0
PLSQL流程控制

PLSQL流程控制

?


if判断
declare
?????? v_b boolean:=true;
begin if v_b then
???????? dbms_output.put_line('ok');
????? end if;
end;

?

if else判断
declare
??????? v_b boolean:=true;
begin
???? if v_b then
??????? dbms_output.put_line('ok');
???? else
??????? dbms_output.put_line('false');
???? end if;
end;

?

if elsif else判断
declare
??????? v_name varchar2(20):='cheng';
begin
???? if v_name='0701' then
??????? dbms_output.put_line('0701');
???? elsif v_name='cheng' then
??????? dbms_output.put_line('cheng');
???? else
??????? dbms_output.put_line('false');
???? end if;
end;

?

loop循环,注意exit是退出循环,而不是退出整个代码块
declare
?????? v_i binary_integer:=0;
begin
???? loop
???????? if v_i>10 then
??????????? exit;
???????? end if;
???????? v_i:=v_i+1;
???????? dbms_output.put_line('hehe');
???? end loop;
???????? dbms_output.put_line('over');
end;

?

loop简化写法
declare
?????? v_i binary_integer :=0;
begin
???? loop
???????? exit when v_i>10;
???????? v_i :=v_i+1;
???????? dbms_output.put_line('hehe');
???? end loop;
???????? dbms_output.put_line('over');
end;

?

while循环
declare
?????? v_i binary_integer:=0;
begin
?????? while v_i<10 loop
???????????? dbms_output.put_line('hello'||v_i );
???????????? v_i:=v_i+1;
?????? end loop;
?????? dbms_output.put_line('over');
end;

?

for循环,注意不需要声明变量
begin
????
for v_i in 0..10 loop
???????? dbms_output.put_line('hello'||v_i);
????
end loop;
???????? dbms_output.put_line('over');
end;

  相关解决方案