oracle 1

来源:互联网 发布:乐视2用不了移动数据 编辑:程序博客网 时间:2024/06/06 09:18

觉得应该好好总结一下,ORACLE之PL/SQL学习
begin
if ... then
...
elsif ...then
...
end if;
end;
实例:
 

Sql代码 复制代码
  1. DECLARE  
  2. a number;   
  3. b varchar2(10);   
  4. begin  
  5. a:=2;   
  6. if a=1 then  
  7. b:='a';   
  8. elsif a=2 then  
  9. b:='b';   
  10. else  
  11. b:='c';   
  12. end if;   
  13. DBMS_OUTPUT.PUT_LINE('b is'||b);   
  14. end;   
  15. /  

有关case when
case
when.. then...
when.. then...
end case
实例
 

Sql代码 复制代码
  1. DECLARE  
  2. a number;   
  3. b varchar2(10);   
  4. begin  
  5. a:=2;   
  6. case    
  7. when a=1 then b:='a';   
  8. when a=2 then b:='b';   
  9. when a=3 then b:='c';   
  10. else  
  11. b:='others';   
  12. end case;   
  13. DBMS_OUTPUT.PUT_LINE('b is'||b);   
  14. end;   
  15. /  


循环语句
LOOP
...
END LOOP

WHILE expression LOOP
...
END LOOP

FOR counter in[REVERSE] start_value..end_value LOOP
...
END LOOP;

实例(用第一种方式)
 

Sql代码 复制代码
  1. declare  
  2. x number;   
  3. begin  
  4. x:=0;   
  5. loop   
  6. x:=x+1;   
  7. if x>=3 then  
  8. exit;   
  9. end if;   
  10. dbms_output.put_line('内:x='||x);   
  11. end loop;   
  12. dbms_output.put_line('外:x='||x);   
  13. end;   
  14. /  

用exit when方式
 

Sql代码 复制代码
  1. declare  
  2. x number;   
  3. begin  
  4. x:=0;   
  5. loop   
  6. x:=x+1;   
  7. exit when x>=3;   
  8. dbms_output.put_line('内:x='||x);   
  9. end loop;   
  10. dbms_output.put_line('外:x='||x);   
  11. end;   
  12. /  

用第三种方式,注意这是reverse的俄,如果是1 to 5,就不加reverse
 

Sql代码 复制代码
  1. begin  
  2. for i in reverse 1..5  loop   
  3. dbms_output.put_line('i='||i);   
  4. end loop;   
  5. dbms_output.put_line('end of for loop');   
  6. end;   
  7. /  



 

原创粉丝点击