故如虹,知恩;故如月,知明
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

oracle编程基础

4489人阅读 2018/12/28 21:28 总访问:3943341 评论:0 收藏:0 手机
分类: Oracle

简单介绍一下oracle中if,else,case when,循环,异常处理等用法


if,else

declare ptype int:=2;
begin
    if ptype = 1 then
       dbms_output.put_line('长');
    else
       dbms_output.put_line('短');
    end if;
end;

if,else if ,else

declare ptype int:=3;
begin  
    if ptype = 1 then
       dbms_output.put_line('长');
    elsif ptype = 2 then  --注意是elsif 不是else if
       dbms_output.put_line('粗');
    elsif ptype = 3 then
       dbms_output.put_line('壮');
    else
       dbms_output.put_line('短');
    end if;
end;


Case When

declare ptype int;
begin  
    ptype:=5;
    case ptype
      when 1 then
          dbms_output.put_line(1);
      when 2 then
          dbms_output.put_line(2);
      else
          dbms_output.put_line('sdfdsf');
     end case;
end;

方法2:

declare ptype int;
begin  
    ptype:=1;
    case 
      when ptype = 1 then
          dbms_output.put_line('香蕉');
      when ptype = 2 then
          dbms_output.put_line('拔那');
      else
          dbms_output.put_line('其他');
     end case;
end;

在查询语句中使用case when

select  ename,
   ( 
     case when ename='SCOTT' then '(>^ω^<)喵'
     when ename = 'BLAKE' then '公老虎'
     when ename = 'KING' then '母老虎'
     else '纸老虎' end   --注意这里是end不是end case
   ) 
from scott.emp;



Oracle循环


1.loop循环

declare i int;
begin
     i:=1;
     loop
         dbms_output.put_line(i);  
         exit when i >=10;
         i:=i+1;--循环退出条件    
     end loop;     
end;

2.while循环

declare ptype int;
begin
     ptype:=1;
     while ptype<10
     loop
         dbms_output.put_line(ptype);  
         ptype:=ptype+1;    
     end loop;     
end;

3.for循环

declare ptype int;
begin
     ptype:=1;
     for ptype in 1..10 
     loop
         dbms_output.put_line(ptype);  
     end loop;     
end;


Oracle异常处理

declare 
 i int:=1;
 p nvarchar2(64);
begin  
   p:='hello';
   i :=p+1; --让数字和字符串相加模拟一个错误
   dbms_output.put_line(p);
   exception 
     when value_error  then  --捕获数字或值错误
        dbms_output.put_line('数字或值错误');
     when others then
        dbms_output.put_line('报错了啊'); 
end;






欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价