PLSQL-存储过程
【无返回值,有 out 型参数】--定义一个存储过程,获取给定部门的工资总和(通过 out 参数)。--要求:部门号和工资总额定义为参数create or replace procedure get_sal2(dept_id number,sumsal out number)ascursor salary_cursor is select salary from employees where d
·
存储过程无返回值,创建的时候不用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)