1.存储过程
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 as result_cursor type_cursor; begin open result_cursor for select stu_id,stu_num from student where mod(stu_num,2)=0; return result_cursor; end p_cursor; end ceshi_package;
调用
declare tt_1 sys_refcursor; tt_id number; tt_num number; begin -- ceshi_procedure(tt_1); 过程 -- tt_1:=ceshi_cursor; 方法 tt_1:=ceshi_package.p_cursor;包方法 --open tt_1; 不用打开的 fetch tt_1 into tt_id,tt_num; while tt_1%found loop dbms_output.put_line(to_char(tt_id)); dbms_output.put(to_char(tt_num)); fetch tt_1 into tt_id,tt_num; end loop; close tt_1; end;
四、Sqlserver中oracle中如何判断表或者存储过程是否存在
Sqlserver中可以这样 ====这个是用来判断存储过程是否存在的 if exists(select * from sysobjects where name='test2' and type='P') drop procedure test2 go ====这个使用来判断数据库的表是否存在 if exists(select * from master..sysdatabases where name='test2') drop procedure test2 go ==== ==== oracle 中如何来实现 SELECT * FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = UPPER('SCHEMA_NAME') AND OBJECT_NAME = UPPER('TABLE_NAME'); -------------------------------------------------------------- SELECT * FROM ALL_TABLES WHERE OWNER = UPPER('SCHEMA_NAME') AND TABLE_NAME = UPPER('TABLE_NAME');