PL/SQL块 基础

-----查询scott.emp表
select * from scott.emp;
    --完整的PL/SQL块
       --PL/SQL块由三部分组成:定义部分、执行部分、异常部分
    --例子:
    declare --定义(可选)
      v_ename varchar2(20);
      begin--开始标志(必须)
        select ename into v_ename from scott.emp where empno=&empno;---弹出提示框
        dbms_output.put_line('输入员工编号:'||&empno);---显示输入的值
        dbms_output.put_line('员工姓名:'||v_ename);
        exception ----异常处理部分(可选)
          when no_data_found then
            dbms_output.put_line('请输入正确的员工');
      end;---结束标志(必须)
      
      
      
  ---计算员工所得税
  ---知识点:PL/SQL数据类型
  declare
        v_ename varchar2(20);---也可写成 v_ename scott.emp.ename%type
        v_sal number(6,2);
        c_tax_rate constant number(3,2):=0.03;
        v_tax_sal number(6,2);
  begin
    select ename,sal into v_ename,v_sal from scott.emp where empno=&empno;
    v_tax_sal:=v_sal*c_tax_rate;
    dbms_output.put_line('员工编号:'||&empno);
    dbms_output.put_line('员工姓名:'||v_ename);
    dbms_output.put_line('员工工资:'||v_sal);
    dbms_output.put_line('员工所得税:'||v_tax_sal);
        exception 
          when no_data_found then
            dbms_output.put_line('请输入正确的员工编号');
  end;
  
  
  
  select * from scott.emp;
 --按照不同的部门更新员工的工资
 --知识点:条件分支语句if……then……  elsif……then……  else……
 declare 
    v_dep number(6,2);
    v_sal number(6,2);
 begin
   select deptno,sal into v_dep,v_sal from scott.emp where ename=trim('&ename');
   --判断
   if v_dep=10 then
     dbms_output.put_line('v_dep=10');
   elsif v_dep=20 then
         dbms_output.put_line('v_dep=20');
   else
     dbms_output.put_line('其他');
   end if;--分支语句结束标志
    exception 
      when no_data_found then
        dbms_output.put_line('数据错误');
 end;
 
 ----更新相应部门的员工的补贴
 ----知识点:case语句的使用
 declare
        v_deptno scott.dept.deptno%type;
 begin
   v_deptno:=&deptno;
   --case语句
   case v_deptno
     when 10 then
       --v_deptno=10,则更新员工补贴为100
       update scott.emp set comm=100 where deptno=v_deptno;
     when 20 then 
       --v_deptno=20,则更新员工补贴为80
       update scott.emp set comm=80 where deptno=v_deptno;
      else
        dbms_output.put_line('不存在该部门');
      end case;---结束
      ---或
      -------使用exception抛异常
       -- exception
          --when no_data_found then
           -- dbms_output.put_line('输入不正确');

 end;
  select * from scott.emp;
 
 
 
 --循环语句
        --1、基本循环
        declare 
          i number(4);
        begin
          --给i赋初始值1
          i:=0;
          --开始循环
          loop
            --i自加1
            i:=i+1;
            --输出
            dbms_output.put_line('输出的第'||i||'个数:'||i);
            exit when i=10;--循环退出的条件
          end loop; --结束循环标志
        end;
        
        --2、while循环
        declare 
          i number(4);
          begin
            --给i赋初始值
            i:=0;
            --whie循环
            while i<10 loop
              i:=i+1;--自加1
              dbms_output.put_line('输出的第'||i||'个数:'||i);
              end loop;--结束循环标志
          end;
          
          --3、for循环
          declare 
              i number(4);--声明变量
              begin
                --for循环
                for i in 1..10 loop
                  dbms_output.put_line('输出的第'||i||'个数:'||i);
                end loop;--循环结束标志
              end;
 
 -----异常处理
         --1、处理预定义异常
            --case_not_found,在cas语句中,如果在when子句中没有包含必须的条件分支,有无else语句,则就会隐含触发该异常
            
            --cursor_already_open,当打开已经打开的游标时,会隐含触发该异常
            
            --invalid_number,当不能有效的把字符转化为数字时,会隐含触发该异常
            
            --too_many_rows,当执行select into 子句时,如果返回超过一行就会隐含触发该异常
            
            --zero_divide,如果除数为0,会隐含触发该异常
            
            --no_data_found,当执行select into 未返回行,或者引用了索引表未初始化元素,会隐含触发该异常
            
            --例子:zero_divide
                    declare
                        dnum number(4):=10;
                    begin
                      dbms_output.put_line(dnum/0);
                      exception 
                        when zero_divide then
                          dbms_output.put_line('不能除以0');
                    end;
          --2、自定义异常
                    --自定义异常是指PL/SQL开发人员所定义的异常。自定义异常必须是显示的触发,使用自定义异常的步骤包括
                    --a.定义异常   b.显示触发异常   c.引用异常
                    
                    --例子:
                    declare
                               e_integerity exception;---定义非预定义异常
                               e_no_employee exception;--定义自定义异常 
                               pragma exception_init(e_integerity,-2291);--关联非预定义异常
                    begin
                      update scott.emp set deptno=40 where empno=-1;
                          if sql%notfound then
                            raise e_no_employee;---显示触发自定义异常
                          end if;
                      exception
                        when e_integerity then
                          dbms_output.put_line('该部门不存在');
                        when e_no_employee then
                          dbms_output.put_line('该员工不存在');
                    end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值