---
title: "存储过程（PROCEDURE）"
author: "jetwill"
date: "2010-08-08T18:43:50Z"
tags: []
categories: ["数据库"]
slug: "procedure"
draft: false
lastmod: "2010-08-08T20:47:14Z"
id: 732178
url: "https://jetwill.iteye.com/blog/732178"
---

存储过程的语法

```sql

CREATE OR REPLACE PROCEDURE 存储过程名
[　(参数变量1 [ in | out | int out ] 数据类型,
:
 参数变量n  [ in | out | int out ] 数据类型）　]　{ AS | IS }

/*　声明部分 */
BEGIN
/*　可执行部分 */
EXCEPTION
/*　异常处理部分 */
END [ 存储过程名 ];

```


与匿名块不同的是，存储过程无需DECLARE关键字。取而代之的是IS或者AS关键字。

给参数指定参数类型时，不可指定长度。
例）
×　param1 in char(2)
○　param1 in char

●参数的模式
IN
启动存储过程时作为只读参数传入，在存储过程内不可更改。

OUT
启动存储过程时作为参数传入，忽视其值。
在存储过程内可以设置它的值，当存储过程结束时作为返回值返回到调用方。

IN OUT
IN和OUT的组合模式。
启动存储过程时作为参数传入，在存储过程内设置该参数的值，
在存储过程结束时则作为返回值返回到调用方。



示例1 由参数指定键值，获取表的，返回。

```sql

create or replace procedure get_dept (
　v_dept_id in char,
　v_dept_name out varchar2) as
 
begin
 
　select dept_name into v_dept_name from dept
 　　where dept_id = v_dept_id;
 
end get_dept;

```


示例2 以匿名块方式执行示例1的存储过程

```sql

declare
 
　disp_dept_name varchar2(10);
 
begin
 
　get_dept(v_dept_id => 'D01',v_dept_name  => disp_dept_name);
　dbms_output.put_line(disp_dept_name);
 
end;

```


→[匿名块](http://jetwill.iteye.com/blog/732177)
→[存储过程（PROCEDURE）](http://jetwill.iteye.com/blog/732178)
→[存储函数（FUNCTION）](http://jetwill.iteye.com/blog/732193)
→[存储包（PACKAGE）](http://jetwill.iteye.com/blog/732195)
→[触发器（TRIGGER）](http://jetwill.iteye.com/blog/732196)
→[PL/SQL的数据类型](http://jetwill.iteye.com/blog/732215)
→[PL/SQL的运算符（计算、赋值、结合、连结、指数、比较）](http://jetwill.iteye.com/blog/732221)
→[PL/SQL的变量和常量](http://jetwill.iteye.com/blog/732223)
→[%TYPE属性和%ROWTYPE属性](http://jetwill.iteye.com/blog/732224)
→[PL/SQL的内置函数](http://jetwill.iteye.com/blog/732225)
→[PL/SQL的执行权限（AUTHID）](http://jetwill.iteye.com/blog/732226)
→[以独立事务执行PL/SQL](http://jetwill.iteye.com/blog/732228)

→PL/SQL
→首页