存储函数和触发器

--存储函数和存储过程
oracle提供了可以把pl/sql程序存储在数据库中,并可以在任何地方来运行它,这就叫做存储过程或
函数.
过程和函数的唯一区别是函数总向调用者规范数据,而过程则不返回数据

--触发器
触发器是许多关系型数据库系统都提供的一项技术,在oracle系统里,触发器类似于过程和函数,都有
声明执行和异常处理过程的pl/sql块

触发器在数据库以独立的对象存储,它与存储过程不同,存储过程通过其他程序来启动或直接启动运
行,而触发器由一个事件启动运行,即触发器是当某个事件发生时自动地隐式运行

oracle事件:对数据库的表进行增删改查等操作.

触发器的组成
.触发事件:即在某种情况下触发Trigger
.触发时间:即在触发时间发生前还是之后触发
.触发器本身:即触发器本身的目的和意图
.触发频率:说明触发器内定义的动作被执行次数

--函数的helloworld:返回一个"helloworld"字符串
--create or replace function hello_world
--return varchar2
--is begin 
--return'helloworld';
--end;
--调用函数
--begin 
   --dbms_output.put_line(hello_world);
--end;

--返回当前时间
--create or replace function date_demo
--return DATE
--is v_date date
--begin
--  v_date:=sysdate;
 -- return v_date;
--end;

--begin 
--   dbms_output.put_line(v_date);
--end;


--定义函数,读取给定部门的工资总和,要求:部门号定义为参数,工资总额定义为返回值
--使用cursor游标
/*create or replace function get_sql(deptno_id number)
return number
is 
v_sumsal number(10,2):=0;
--使用游标(返回的值有多条)
  cursor salary_cursor is select salary from employee where department_id = deptno_id;
begin
--开始书写逻辑
  for c in salary_cursor loop
     v_sumsal:=v_sumsal+c.salary;
  end loop;
  return v_sumsal;
end;*/

begin 
  dbms_output.put_line(get_sql(8001));
end;

猜你喜欢

转载自blog.csdn.net/CXY_ZPH/article/details/86571439