极度郁闷,有一段时间没登陆51了,今天回来竟然把提示问题给弄丢了,猜了半个小时才猜出来。。。。

数据库之存储过程

上一篇 / 下一篇  2010-01-07 08:32:55 / 个人分类:Oracle

储存过程是执行某些操作的子程序,它是执行特定任务的模块.
从根本上讲,过程就是命名的 PL/SQL 块,它可以被赋予参数,储存在数据库中由一个应用程序或其它 PL/SQL 程序调用.

--创建存储过程格式

create [or replace] procedure procedure_name [(parameter_list)]
{is | as}

begin
Executable_Statements;
[Exception
Exception_handlers;]
end;

--示例

set serveroutput on;  --打开输出流

--不带参数的存储过程

create or replace procedure insert_dept
is
begin
insert into dept values(60, 'haha', 'haha');
if sql%found then
dbms_output.put_line('插入成功!');
else
dbms_output.put_line('插入失败!');
end if;
end;
/

execute insert_dept;  //execute insert_dept();

--带输入参数的存储过程(参数 dept_no 默认为输入参数即 in 类型)

create or replace procedure find_dept(dept_no number) 
is
dept_name varchar(14);
begin
select dname into dept_name from dept where deptno = dept_no;
dbms_output.put_line('编号为:' || dept_no || ' 的部门是:' || dept_name);
exception
when no_data_found then
dbms_output.put_line('部门编号未找到!');
end;
/

execute find_dept(50);

--带输入和输出参数的存储过程

create or replace procedure test(temp1 in number, temp2 out number)
is
emp_sal number;
begin
select sal into emp_sal from emp where empno = temp1;
if emp_sal < 1500 then
temp2 := 1500;
else
temp2 := 3000;
end if;
exception
when no_data_found then
dbms_output.put_line('员工编号未找到!');
end;
/

declare
value2 number;
begin
test(&empno, value2);
dbms_output.put_line('value2 的值为:' || value2);
end;
/

--带输入输出参数的存储过程(即 in out)

create or replace procedure swap(num1 in out number, num2 in out number)
is
temp number;
begin
temp := num1;
num1 := num2;
num2 := temp;
end;
/

declare
num1 number := 100;
num2 number := 200;
begin
swap(num1, num2);
dbms_output.put_line('num1 = ' || num1);
dbms_output.put_line('num2 = ' || num2);
end;
/

--存储过程授权

1. grant execute on swap to martin;  --将执行过程 swap 的权限授予 martin 用户

2. grant execute on swap to public;  --将执行过程 swap 的权限授予所有用户

--删除过程

格式:drop procedure procedure_name;

示例:drop procedure swap;

TAG: 数据库 存储过程

 

评分:0

我来说两句

Open Toolbar