PL/SQL程序控制结构:
条件控制:
if语句:
if 条件1 then
如果条件1为真执行这里的语句……
elsif 条件2 then
如果条件2为真执行这里的语句……
else
如果条件1和2都不为真则执行这里的语句……
end if;
case语句:
case selector
when 条件1 then
如果条件1为真执行这里的语句……
when 条件2 then
如果条件2为真执行这里的语句……
else
果条件1和2都不为真则执行这里的语句……
end case;
case表达式还可以作用于sql语句中。
循环控制:
loop循环:
declare
v_num number :=1;
begin
loop
dbms_output.put_line(v_num);
v_num:=v_num+1;
exit when v_num>=10;
end loop;
end;
while循环:
declare
v_num number :=1;
begin
while v_num<10
loop
dbms_output.put_line(v_num);
v_num:=v_num+1;
end loop;
end;
for循环:
begin
for v_num in 1..10
loop
dbms_output.put_line(v_num);
end loop;
end;
异常处理:
异常:
declare
v_name emp.ename%type;
begin
select ename into v_name from emp where empno=&输入员工编号;
dbms_output.put_line(v_name);
dbms_output.put_line(33/0);
exception
when no_data_found then
dbms_output.put_line('没有对应的员工');
dbms_output.put_line('继续');
when zero_divide then
dbms_output.put_line('被除数不能为0');
end;
自定义异常:
declare
v_num number;
v_e1 exception;
begin
if v_num is null then
raise v_e1;
end if;
exception
when v_e1 then
dbms_output.put_line('你错了');
end;
传播异常:
begin
raise_application_error(-20001, '你就是错了,出去');
end;
declare
v_name emp.ename%type;
begin
select ename into v_name from emp where empno=&输入员工编号;
dbms_output.put_line(v_name);
dbms_output.put_line(33/0);
exception
when others then
dbms_output.put_line(sqlerrm);
end;
所有的异常都可以用others捕获,用sqlerrm处理。