Oracle使用手册(二)流程控制

--流程控制
--
1. if..then..end if 

set serveroutput on 
declare 
integer:=60;
integer:=30;
begin 
if i>=then
dbms_output.put_line(j);
end if;
end;

--2.if嵌套条件控制

if 条件1 then
   
if 条件2 then
     语句段1;
   
else
     语句段2;
   
end if;
else
   语句段3;
end if;

--3.循环控制 
--
loopexitend loop

set serveroutput on
declare 
 i 
integer:=10;
 j 
integer:=20;
 w 
integer:=10;
 
begin 
 loop 
    i:
=i+1;
    
if(j=i)then
        
begin
         dbms_output.put_line(
'我哥俩现在一样多了.'|| to_char(j)) ; 
         
exit;  
        
end;
     
else
     w:
=w-1;
     
end if;
 
end loop;
   dbms_output.put_line(
'w还剩多少: '|| to_char(w)) ; 
 
end
 
--4.whileloopend loop
while 条件 loop
   执行语句段;
end loop;   

--5.forinloopend
for 循环变量 in[reverse] 循环下界循环上界 loop
     循环处理语句;
end loop;

set serveroutput on
declare
 i 
int:=0;
 j 
int:=100;
begin
  
for j in 1..6 loop
  i:
=i+1;
  
end loop;
   dbms_output.put_line(
'循环次数: '|| to_char(i)) ; 
end;

--5.事务处理
--
在pl/sql中,可以使用三个事务处理控制命令.
--
在PL/SQL程序打开自动提交功能。这样每次执行PL/SQL程序都会自动进行事务提交。
--
语句:   set auto on;
--
相应的,取消自动提交功能的PL/SQL程序为:
--
set auto off;

--commit,rollback,savepoint
--
select * from student;
--
commit;
--
delete from student;
--
rollback; --回滚刚才的delete 操作.
--
select * from student;

savepoint insertpoint; 
insert into student(sid,sname) values(900,'mrfu');
--创建保存点 : savepoint 保存点名;
select sid,sname from student where sid=900;
rollback to insertpoint; --回滚保存点: rollbacd to 保存点名;
--
dbms_output.put_line('回滚保存点') ; 
select sid,sname from student where sid=900;

注意在编写脚本时,要注意添加 begin .... end; 以控制语句执行的起始/终点;否则将会出现语法错误提示;

如:

Code
原文地址:https://www.cnblogs.com/furenjun/p/oracleAffairControl.html