为了账号安全,请及时绑定邮箱和手机立即绑定

Oracle数据库开发必备利器之PL/SQL基础

难度初级
时长 3小时22分
学习人数
综合评分9.57
114人评价 查看评价
9.8 内容实用
9.4 简洁易懂
9.5 逻辑清晰
  • /* 案例2:涨工资问题,从最低工资的员工开始涨起,没人涨10%,工资总额不能超过50000,返回涨工资的人数和涨后的工资总额 分析: 1、用到的sql语句: select empno,sal from emp order by sal; select sum(sal) into totalsal from emp; 2、需要声明的变量: 工资总额:totalsal 涨工资人数:count 3、循环推出的条件: 工资总额>5W or 全部员工都涨完工资 */ declare cursor cemp is select empno,sal from emp order by sal; p_no emp.empno%type; p_sal emp.sal%type; countemp number:=0;--涨工资人数 totalsal emp.sal%type; begin --获取初始工资总额 select sum(sal) into totalsal from emp; open cemp; --判断当前工资总额是否大于5W if totalsal<50000 then loop fetch cemp into p_no,p_sal; exit when cemp%notfound; --获取当前员工涨工资后的工资总额 --如果工资总额超过5W直接退出循环 exit when (totalsal+p_sal*0.1)>50000; update emp set sal=sal*1.1 where empno=p_no; --涨工资人数加1 countemp:=countemp+1; end loop; end if; close cemp; commit; dbms_output.put_line('共有'countemp'人涨工资,工资总额为:'totalsal); end; /
    查看全部
  • --案例1:统计每年入职的员工人数,80,81,82,87年的 declare --定义一个游标存放所有员工的入职年份 cursor c_dates is select to_char(hiredate,'yyyy') from emp; p_hiredate varchar2(20); p_80num number :=0; p_81num number :=0; p_82num number :=0; p_87num number :=0; begin open c_dates; loop fetch c_dates into p_hiredate; exit when c_dates%notfound; if p_hiredate ='1980' then p_80num:=p_80num+1; elsif p_hiredate ='1981' then p_81num:=p_81num+1; elsif p_hiredate ='1982' then p_82num:=p_82num+1; elsif p_hiredate ='1987' then p_87num:=p_87num+1; end if; end loop; close c_dates; dbms_output.put_line('Total:'(p_80num+p_81num+p_82num+p_87num)); dbms_output.put_line('80入职的:'p_80num); dbms_output.put_line('81入职的:'p_81num); dbms_output.put_line('82入职的:'p_82num); dbms_output.put_line('87入职的:'p_87num); end; /
    查看全部
  • --系统例外:zero_divide 被0除 declare pnum number; begin pnum:=1/0; exception when zero_divide then sys.dbms_output.put_line('0不能做除数'); sys.dbms_output.put_line('0真的不能做除数'); when others then sys.dbms_output.put_line('其它例外'); end; /
    查看全部
  • 光标的属性,及更改光标数目限制
    查看全部
  • declare --获取所有部门 cursor c_dept is select deptno from dept; --各部门编号 p_dno dept.deptno%type; --各部门总金额 p_totalsal number; --各工资分段人数: num1 number; num2 number; num3 number; --定义一个游标存放该部门下所有员工(带参数) cursor c_emp(dno number) is select sal from emp where deptno = dno; --员工的薪水 p_sal number; begin --打开部门游标 open c_dept; loop --部门循环 fetch c_dept into p_dno; exit when c_dept%notfound; --初始化变量: p_totalsal:=0; num1:=0; num2:=0; num3:=0; -- --获取本部门下所有员工,打开员工游标 open c_emp(p_dno); loop --员工循环 fetch c_emp into p_sal; exit when c_emp%notfound; if p_sal <3000 then num1:=num1+1; elsif p_sal >=3000 and p_sal<=6000 then num2:=num2+1; elsif p_sal>6000 then num3:=num3+1; end if; --获取总金额 p_totalsal:=p_totalsal+p_sal; end loop; close c_emp; --保存统计结果到sal_msg insert into sal_msg values(p_dno,num1,num2,num3,p_totalsal); end loop; close c_dept; commit; dbms_output.put_line('统计完成'); end; /
    查看全部
  • /* 案例3:将emp表中的员工按部门分段(-3000,3000-6000,6000+)统计各工资段人数和各部门的工资总额 分析: 1、sql语句: --获取所有部门:select deptno from dept; --获取每个部门下的员工:select emono,sal from emp where deptno = p_dno; 2、需要声明的变量: 各部门编号p_dno; 各部门工资总额:totalsal; 各部门每个分段的人数: num1; num2; num3; */
    查看全部
  • --系统例外:value_error算术或转换例外 declare pnum number; begin pnum:='abd'; exception when value_error then sys.dbms_output.put_line('算术或者转换错误'); when others then sys.dbms_output.put_line('其它例外'); end; /
    查看全部
  • --系统例外:too_many_rows; declare pename emp.ename%type; begin select ename into pename from emp where deptno=10; SYS.DBMS_OUTPUT.PUT_LINE(pename); exception when no_data_found then sys.dbms_output.put_line('没有对应的记录'); when too_many_rows then sys.dbms_output.put_line('无法将多行记录赋值给一个普通变量'); when others then sys.dbms_output.put_line('其它例外'); end; /
    查看全部
  • 光标就是一个结果集;<br> 光标的语法: eg: --使用带参数的光标 declare --定义一个带参数的游标 cursor c_emp(dno number) is select ename from emp where deptno=dno; p_ename emp.ename%type; begin --打开游标时需要参入实参 open c_emp(10); loop fetch c_emp into p_ename; exit when c_emp%notfound; DBMS_OUTPUT.PUT_LINE(p_ename); end loop; close c_emp; end; /
    查看全部
  • pl/sql的程序结构 declare 说明部分 begin 语句序列 exception 例外处理语句 end;
    查看全部
  • ---打开输出开关 set serveroutput on ---打印Hello World declare ---说明部分(变量、光标和例外) begin --程序体 dbms_output.put_line('Hello World'); end;
    查看全部
  • 获取结果
    查看全部
  • 后面部分
    查看全部
  • 实例每年入职员工
    查看全部
  • 开发程序过程,瀑布模型
    查看全部

举报

0/150
提交
取消
课程须知
亲,要学习本门课程只需要熟练使用Oracle的SQL语句就可以了,可以参考慕课网的课程《Oracle数据库开发必备利器之SQL基础》呦!
老师告诉你能学到什么?
1、能够熟练掌握PL/SQL的基本语法 2、能够熟练使用光标和例外 3、能够熟练使用PL/SQL进行开发

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!