先来一个简单的存储过程样式--更新某员工的工资,并打印更新之前和更新之后的数据;
create or replace procedure updateEmpSal( en in number,psal in number)
as
oldSal emp.sal%type;
newSal emp.sal%type;
begin
select sal into oldSal from emp where empno = en;
dbms_output.put_line('更新之前:’|| oldSal);
update emp set sal = sal + psal where empno = en;
commit;
select sal into newSal from emp where empno = en;
dbms_output.put_line('更新之后:’|| newSal);
end;
-----加上注释和数据名称
创建 或 更新 存储过程 过程名称 (参数1 和类型;in为输入参数,out输出参数)
as
oldSal emp的工资为此赋值;
newSal emp的工资为此赋值;
begin
--查询并打印更新之前的工资
--更新工资
--查询并打印更新之后的工资
end;
----访问存储过程
--call updateEmpSal (123,500);--123为员工号 --500 为增加的工资;
2、
----举例: 根据员工编号得到年薪
create or replace procedure getYearSalByEmpno(eno in number ,yearsal out number)--out代表此参数是输出参数
as
begin
select sal * 12 + nvl(comm ,0) into yearsal from emp where empno = eno;--nvl(p1,p2)--如果p1为空取值p2;
end;
-- 访问带有输出参数的存储过程
declare
ys number; --ys为参数名
begin
getYearSalByEmpno (7788,ys);
dbms_output.put_line(ys); --打印员工的年薪
end;
--08-05-22 待增 sasha
本文介绍两个Oracle存储过程示例:一是更新员工工资并显示更新前后的薪资;二是根据员工编号计算年薪。通过具体代码实现,展示了存储过程在数据库操作中的应用。
3万+

被折叠的 条评论
为什么被折叠?



