`
vipbooks
  • 浏览: 136890 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类

Oracle学习笔记(3) PLSQL程序控制结构

阅读更多
  这是第三章的学习笔记,学习完第二章的编程基础之后,从现在开始要学习Oracle编程了……,希望大家能多给俺一些支持啊!
   这周六总算是不用加班,可以好好出去玩一下了!
   今天去武大看樱花了,哈哈,不错!   

    编程时使用的工具是PLSQL Developer 7.1.4
 

select * from employee;
select * from dba_tab_cols t where t.table_name='EMPLOYEE';


declare
   n_empno employee.empno%type;
   v_name employee.name%type;
   n_salary employee.salary%type;
   v_temp varchar2(30);
   n_temp number(5) := 1;
   -- 自定义异常
   e_exception exception;

exception_init 是一个编译时指令,用于将一个内部错误与异常的名称关联。
一旦关联成功后,我们就可以通过名称抛出异常并用when处理器捕获错误
   pragma exception_init(e_exception,-66666);
   
begin
   n_empno := &员工编号:;
   select name,salary into v_name,n_salary from employee where empno=n_empno;

if 语句
简单条件判断
   if n_salary < 4000 then
      update employee set salary = n_salary+500 where empno=n_empno;
      dbms_output.put_line(v_name || '的工资增加了500元!');
   else
      dbms_output.put_line(v_name || '的工资大于4000,不用加工资!');
   end if;
   -- 多重条件分支
   if n_salary < 4000 then
      dbms_output.put_line(v_name || '的工资少于4000元!');
   elsif n_salary >= 4000 and n_salary < 5000 then
      dbms_output.put_line(v_name || '的工资在4000~5000元之间!');
   elsif n_salary >= 5000 and n_salary < 6000 then
      dbms_output.put_line(v_name || '的工资在5000~6000元之间!');
   else
      dbms_output.put_line(v_name || '的工资大于6000元!');  
   end if;

case 语句
使用单一选择符进行等值比较
   case n_salary
      when 4200 then
         dbms_output.put_line(v_name || '的工资是4200元!');
      when 3500 then
         dbms_output.put_line(v_name || '的工资是3500元!');
      when 6800 then
         dbms_output.put_line(v_name || '的工资是6800元!');
      else
         dbms_output.put_line(v_name || '的工资是' || n_salary ||'元!');
    end case;
    -- 使用多条件比较
    case
       when n_salary < 4000 then
            dbms_output.put_line(v_name || '的工资少于4000元!');
       when n_salary < 5000 then
            dbms_output.put_line(v_name || '的工资少于5000元!');
       when n_salary < 6000 then
            dbms_output.put_line(v_name || '的工资少于6000元!');  
       else
            dbms_output.put_line(v_name || '的工资大于6000元!');
    end case;

case 表达式
使用单一选择符
    v_temp := case n_salary
                  when 3500 then v_name || '的工资是3500元!'
                  when 4200 then v_name || '的工资是4200元!'
                  when 6800 then v_name || '的工资是6800元!'
              else
                  v_name || '的工资是' || n_salary ||'元!'
              end;
    dbms_output.put_line('v_temp:' || v_temp);
    -- 使用多条件比较
    v_temp := case
                 when n_salary < 4000 then 4000
                 when n_salary < 5000 then 5000
                 when n_salary < 6000 then 6000
                 else
                      n_salary
              end + 500;
     dbms_output.put_line(v_name || '的工资原工资是' || n_salary || ' 调整工资并增加500元奖金后的结算工资是 ' || v_temp);                    
     -- 将case 语句用在SQL语句中
     select case 
            when manager = 1 then '经理'
            else '职员'
            -- 是经理还是职员 表示列的别名,也可以不写,在这里不要打引号
            end 是经理还是职员 into v_temp 
            from employee where empno = n_empno;
     dbms_output.put_line(v_name || '是:' || v_temp);

循环控制
基本循环
     loop
        -- 程序在这里至少会执行一次,注意一定要加上exit when……结束循环的条件,否则程序会进入死循环
        exit when n_temp >= 100;
        n_temp := n_temp + 1;
     end loop;
     dbms_output.put_line('n_temp loop循环100次累加的结果是:' || n_temp);
     -- while 循环
     while n_temp <= 100 loop
        n_temp := n_temp + 1;
     end loop;
     dbms_output.put_line('n_temp while循环100次累加的结果是:' || n_temp);

for 循环
如果指定了reverse 选项,那么每次循环时变量会递减,否则变量会递加,注意循环中用的是两个"."
     for i in reverse 1..100 loop
         dbms_output.put_line('for循环中I的值:' || i);
     end loop;
     -- 嵌套循环和标号(Lable)
     -- 定义一个标号
     <<outer>>
     for i in 1..100 loop
         <<inner>>
         for j in 1..100 loop
             n_temp := i*j;
             -- 如果n_temp=1000 就直接退出外层循环
             exit outer when n_temp = 1000;
             -- 如果n_temp=500 只会退出内层循环
             exit inner when n_temp = 500;
         end loop inner;     
         dbms_output.put_line('内层:' || n_temp);
     end loop outer;
     dbms_output.put_line('外层:' || n_temp);

顺序控制
goto 语句用于跳转到特定标号处去执行语句,一般不建议使用
     for i in 1..100 loop
         if i > 50 then
            goto end_loop;
         end if;
         dbms_output.put_line(i);
     end loop; 
     <<end_loop>>
     dbms_output.put_line('loop 循环了50次提前结束了!' );
     -- NULL 语句,它不会执行任何操作,并且会将控制直接传到下一条语句
     if n_salary > 5000 then
        dbms_output.put_line(v_name || '的工资大于4000');
     else 
        null;
     end if;

异常处理
在要处理抛出的异常的异常处理部分的when子句中引用raise e_exception;
用raise_application_error替代raise ,我们可以将错误消息与异常关联在一起
错误号必须是在-20000到-20999之间的负整数;错误描述的长度不能超过2048字节;
第三个参数为可选参数,如果设置为true,则该错误号被放在先前的错误堆栈中;
如果设置为false(默认值),则会替换先前所有错误
     raise_application_error(-20001,'这里有Application异常!',true);
exception
     -- 系统异常
     when NO_DATA_FOUND then dbms_output.put_line(sqlerrm);
     -- 自定义异常
     /*
        异常名只能在两个地方引用:
        一、在要抛出异常的程序执行部分用RAISE语句引用:如:raise e_exception;
        二、在要处理抛出的异常的异常处理部分的when子句中引用
     */
     when e_exception then dbms_output.put_line('这里是自定义异常:' || sqlerrm);
     /*
        使用异常函数:
        函数sqlcode用于取得Oracle错误号,而sqlerrm则用于取得与之相关的错误消息
     */
     when others then dbms_output.put_line('错误号:' || sqlcode || '   错误消息:' || sqlerrm);
end;
/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics