准备的数据如下:
存在一个问题,当奖金为空的时候,算出来的年收入竟然是空的。
因为 如果一个表达式中有空值,那么这个表达式的结果即为空值。
所以我们需要对空值进行处理, 使用nvl函数即可。
最后修改后的function为
create or replace function querySalaryInCome(staffName in varchar2) return number as --定义变量保存员工的工资和奖金 pSalary xgj_test.sal%type; pComm xgj_test.comm%type; begin --查询员工的工资和奖金 select t.sal, t.comm into pSalary, pComm from xgj_test t where t.username = staffName; --直接返回年薪 return pSalary * 12 + nvl(pComm,0); end querySalaryInCome; out参数 out参数一般来讲,存储过程和存储函数的区别在于存储函数可以有一个返回值,而存储过程没有返回值。
存储过程和存储函数都可以有out参数
存储过程和存储函数都可以有多个out参数
存储过程可以通过out参数实现返回值
那我们如何选择存储过程和存储函数呢?
原则:
如果只有一个返回值,用存储函数,否则(即没有返回值或者有多个返回值)使用存储过程。
/** 根据员工姓名,查询员工的全部信息 */ create or replace procedure QueryStaffInfo(staffName in xgj_test.username%type, pSal out number, pComm out xgj_test.comm%type, pJob out xgj_test.job%type) is begin --查询该员工的薪资,奖金和职位 select t.sal,t.comm,t.job into pSal,pComm,pJob from xgj_test t where t.username=staffName; end QueryStaffInfo;先抛出两个思考问题:
查询员工的所有信息–> out参数太多怎么办?
查询某个部门中所有员工的信息–> out中返回集合?
后面会讲到如何解决? 总不能一个个的写out吧~
在应用中访问存储过程和存储函数 概述我们使用JAVA程序连接Oracle数据库。
使用jar: ojdbc14.jar
关于oracle官方提供的几个jar的区别
classes12.jar (1,600,090 bytes) - for use with JDK 1.2 and JDK 1.3
classes12_g.jar (2,044,594 bytes) - same as classes12.jar, except that classes were compiled with “javac -g” and contain some tracing information.
classes12dms.jar (1,607,745 bytes) - same as classes12.jar, except that it contains additional code`to support Oracle Dynamic Monitoring Service.
classes12dms_g.jar (2,052,968 bytes) - same as classes12dms.jar except that classes were compiled with “javac -g” and contain some tracing information.
ojdbc14.jar (1,545,954 bytes) - classes for use with JDK 1.4 and 1.5
ojdbc14_g.jar (1,938,906 bytes) - same as ojdbc14.jar, except that classes were compiled with “javac -g” and contain some tracing information.
ojdbc14dms.jar (1,553,561 bytes) - same as ojdbc14.jar, except that it contains additional code`to support Oracle Dynamic Monitoring Service.
ojdbc14dms_g.jar (1,947,136 bytes) - same as ojdbc14dms.jar, except that classes were compiled with “javac -g” and contain some tracing information.
工程目录如下: