1.存储过程
日期:2014-05-16 浏览次数:20452 次
1.存储过程
2.触发器
3.存储过程和函数的区别
--------------------------------
一、存储过程
?????? 1.基本的语法格式
--格式和定义plsql的函数格式差不多 create or replace procedure 过程名(参数以及返回值和函数定义是一样的) is|as begin code; end 过程名;
?
?????? 2.调用
直接输入存储过程的名字();
?
?二、触发器
?
???? 1.基本格式
?
create or replace trigger 触发器名称
before|after|instead of (操作前|操作后|替代)
delete or inserte or (update (of columu)) (删除 添加 更新 of制定哪些列的跟新)
on 表名、视图 for each row
begin
code;
end 触发器名称;
--------------------例子-----------------------
a和b两个的表结构一样
当a删除内容的时候触发触发器把删除的内容保存到b中
create or replace trigger del_a
before delete on a for each row
begin
insert into b values(:old.id,:old.num....);
end del_a;
?
三、存储过程和函数的区别
?
1.返回值的区别,函数有1个返回值,而存储过程是通过参数返回的,可以有多个或者没有 2.调用的区别,函数可以在查询语句中直接调用,而存储过程必须单独调用. 函数一般情况下是用来计算并返回一个计算结果而存储过程一般是用来完成特定的数据操作(比如修改、插入数据库表或执行某些DDL语句等等)
?
??? 多返回值
---------------------------例子-------------------------------
create or replace procedure ceshi(stu_num out number,stu_type out number)
is
begin
select stu_num,stu_type into stu_num,stu_type from student
where stu_id='2735';
end ceshi;
-------------------------执行--------------------------------
declare
nums number;
cs_type number;
begin
ceshi(nums,cs_type);
dbms_output.put_line(to_char(nums));
dbms_output.put_line(to_char(cs_type));
end;
-----------------------结果-----------------------------
?
?


?
?返回值是一个游标的例子
???????????????????????????????????????????????????????????????? 方法一 系统个人游标 sys_refcursor
在自定义函数中
create function ceshi_cursor
return sys_refcursor
is
tt sys_refcursor;
begin
open tt for select stu_id,stu_num from student
where mod(stu_id,2)=0;
end ceshi_cursor;
?在存储过程中
create procedure ceshi_procedure(tt_cursor out sys_refcursor)
is
begin
open tt for select stu_id,stu_num from student
where mod(stu_id,2)=0;
end ceshi_procedure;
?????????????????????????????????????????????????????? 方法二 包头声明包体使用
create package ceshi_package
is
type type_recode is record(p_id number,p_num number);
type type_cursor is ref cursor return type_recode;
function p_cursor return type_cursor;
end ceshi_package;
------------------------------------------------
create or replace package body ceshi_package
is
function p_cursor return type_cursor