PLSQL-存储过程

存储过程无返回值,创建的时候不用return,执行语句中同样不存在return语句。

【无返回值,有 out 型参数】

--定义一个存储过程,获取给定部门的工资总和(通过 out 参数)。
--要求:部门号和工资总额定义为参数

create or replace procedure get_sal2(dept_id number,sumsal out number)

as

cursor salary_cursor is select salary from employees where department_id = dept_id;

begin

sumsal := 0;

for c in salary_cursor loop

sumsal := sumsal +c.salary;

end loop;

dbms_output.put_line(sumsal);
end;
【调用方式】
declare

v_sal number(10) := 0;

begin
//直接使用procedure名字
get_sal2(80,v_sal);

end;

【存储过程、游标和流程控制综合使用】

/*对指定部门(作为输入参数)的员工进行加薪操作,若其到公司的时间在
{?,95) 加薪5%
[95,98)  加薪3%
[98,?)   加薪1%
得到以下返回结果:为此次加薪公司每月需要额外付出多少成本(定义一个out型的输出参数)。
*/

create or replace procedure add_sal(dept_id number,temp_sal out number)
as
//定义游标
cursor sal_cursor is select employee_id,salary,hire_date from employees where department_id = dept_id;
//定义变量,记录加薪百分比
v_i number(4,2) := 0;
--记录加薪百分比

begin

temp_sal := 0;

for c in sal_cursor loop

if to_char(c.hire_date,'yyyy') < '1995' 
then v_i := 0.05;
elsif to_char(c.hire_date,'yyyy') < '1998' 
then v_i := 0.03;
else v_i :=0.01;
end if;

--1.更新工资
update employees set salary = salary * (1+v_i);
--2.付出的成本
temp_sal := temp_sal + c.salary*v_i;

end loop;

dbms_output.put_line(temp_sal);

end;
【调用方式】

declare

v_sal number(10) := 0;

begin

add_sal(80,v_sal);

end;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值