oracle 存储函数,存储过程


--存储函数
--返回一个'helloword'字符串
create or replace function helloword
 return varchar2
is
--声明部分
begin 
return 'helloword';
end;
--调用存储函数
begin 
dbms_output.put_line(helloword);
end;
--或者用
select helloword fron dual;
--加参的存储函数
create or replace function helloword1(v_log varchar2)
return VARCHAR2
is 
begin 
return 'helloword'|| v_log;
end;
--调用存储函数
select helloword1('xiaoyu') from dual;


--创建一个存储函数,返回当前系统时间 
create or replace function  show_time
return date
is 
begin
return sysdate;
end;


select show_time from dual;
--定义带参数的函数,两个数相加
create or replace function add_two(n1 number,n2 number)
return NUMBER
is 
begin 
return n1+n2;
end;


select add_two(2,3) FROM dual;


--定义一个函数,获取给定部门的工资总和,要求:部门号定义为参数,工资总额定义为返回值


create or replace function emp_sumsal(dept_id number)
return number
is 
v_sal employees.salary%type;
begin 
select sum(salary) into v_sal from employees where department_id= dept_id;
return v_sal;

end;



--定义一个函数 ,获取给定部门的工资总和 和该部门的员工总数(定义为out 类型的参数 ),
--要求部门号定义为参数,工资总额定义为返回值 .
create or replace function get_sal(dept_id number,total_count out number)
return NUMBER
is
v_sal employees.salary% type;


begin 
select sum(salary),count(*) into v_sal, total_count from employees where department_id=dept_id;
dbms_output.put_line(total_count);
return v_sal;
end;
--调用存储过程
declare
v_num number(5);
begin 
dbms_output.put_line(get_sal(80,v_num));
end;
set serveroutput on;



***********************存储过程***********************************

没有返回值 ,一般可以把增删改定义为存储过程 ,查询用存储函数.

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


create or replace procedure get_sal1(dept_id number,sum_sal out number) 
is 
v_sal number(9,2);
begin 
select sum(salary) into v_sal from employees where department_id= dept_id;
dbms_output.put_line(v_sal);
end;
--存储过程调用
declare
v_sal number(9,2):=0;
begin 
get_sal1(80,v_sal);
end;

declare
v_sal number(9,2):=12;
begin 
--这里所传进的参数值,无法影响,存储过程中相对应的值 ,即存储过程中,sum_sal中的值,依旧是0;
get_sal1(80,v_sal);
end;
set serveroutput on;


--对给定部门(作为输入参数)的员工进行加薪操作,若其到公司的时间在(?,95)期间,为其加薪5%,(95,98)3%,(98,?)1%
--得到以下返回结果,为此次加薪公司需要额外付出多少成本(定义一个out型的输出参数);
create or replace procedure add_sal(dept_id number,v_extra_sal out number)
is 
v_temp number(4,2);
cursor emp_date is SELECT hire_date, employee_id,salary from employees1 where department_id= dept_id;
BEGIN
--不能在v-extra_sal不能在 is 中初始化,is中只能在声明的同时初始化,类似于java中,这个情况:
--( public class Test {
-- int a;//正确
    --  int  a=10//正确 成员属性定义的时候可以初始化赋值,若不赋值则自动赋默认值,int = 0 ,long = 0.0 ,对象 = null。
--a=10;//这样是错误的
--} )


--因为v-extra_sal是參數,已經被声明,所以要再进行初始化只能在begin里
  v_extra_sal:=0;
  for c in emp_date loop
  if to_char(c.hire_date,'yyyy')<'1995' then v_temp:=0.05;
  elsif to_char(c.hire_date,'yyyy')>'1995' and to_char(c.hire_date,'yyyy')<'1998' then v_temp:=0.03; 
  else v_temp:=0.01;
  end if;
  UPDATE employees1 set salary= salary*(1+ v_temp) where employee_id=c.employee_id;
  v_extra_sal:= v_extra_sal + c.salary * v_temp;
  end loop;
  dbms_output.put_line(v_extra_sal);
end;


declare
extra_sal NUMBER(10):=0;
begin 
add_sal(80,extra_sal);
end;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值