前言:
转载请附上连接,本帖原创请勿照抄。
存储过程用于完成特定功能的SQL语句集合,编译完后存储在数据库中,不同的程序在执行同一个SQL的时候可以直接调用存储过程无需重新写sql语句。
优点: 模块化程序、需只要调用无需传输SQL,加快SQL执行速度,提高安全性,减少流量,提高可移植性,维护方便。
缺点: 存储过程建立复杂的SQL业务进行过程化执行会导致执行速度变慢,需要应用缓存不太合适,不支持集群。
一.存储过程
存储过程格式(存储过程需要用到的SQL在最底部)
----重复建立存储过程会执行覆盖操作
create or replace procedure 过程名称 [定义参数(in获取参数 out输出参数)]
----在视图中用AS 在游标中用IS
is或者as
----声明变量
begin
----执行代码
end;
举栗
----存储过程说明
----select count(1) into inte from p_shool where p_a=inta; 根据传进来的inta查询shool的p_a字段,如果p_a有值 inte则=1,
----如果p_a没值 inte则=0
----if inte=1 then 判断inte=1 则p_a有名字则进行下一步的Insert into插入该名字拥有的存款
----最后 dbms_output.put_line 返回是否插入存款
----inta id intb money intc ina+intb ret 输出参数number类型(存储过程可以定义多个输出参数)
create or replace procedure csdn_test(inta in int,intb in int,intc out int,ret out number)
AS
inte int;
begin
select count(1) into inte from p_shool where p_a=inta;
if inte=1 then
Insert into m_procedure values(inta,intb);
dbms_output.put_line('输入money');
else
dbms_output.put_line('输入失败');
end if;
end;
调用存储过程:
declare
----定义变量
begin
----调用存储过程
end;
举栗
declare
ret number;
begin
csdn_test(1,1000,0,ret);
end;
删除存储过程
drop procedure 过程名称
需要用到的SQL表
create table m_procedure
(
m_inta int,
m_intb int
)
select * from m_procedure;
create table p_shool
(
p_a int,
p_b varchar(5)
)
Insert into p_shool values(1,'小明');
Insert into p_shool values(2,'小黑');
Insert into p_shool values(3,'小白');
下一篇 Oracle存储函数(附带SQL语句完整过程及示例)
实现MFC连接存储过程并实现调用执行请参考下一篇博客