Home > Database > Mysql Tutorial > 存储过程的简单应用

存储过程的简单应用

WBOY
Release: 2016-06-07 15:23:20
Original
1015 people have browsed it

存储过程的简单应用 存储过程:执行一个任务,该任务包括了一系列的PL SQL语句,存储在数据库中,成为数据库一个对象。效率比较高的,但你创建一个存储过程它会进行一个判断编译的。 =================================== 1.创建一个简单的存储过程 SQL CREA

存储过程的简单应用

存储过程:执行一个任务,该任务包括了一系列的PL SQL语句,存储在数据库中,成为数据库一个对象。效率比较高的,但你创建一个存储过程它会进行一个判断编译的。

===================================
1.创建一个简单的存储过程

SQL> CREATE OR REPLACE PROCEDURE xs_proc
2 IS
3 BEGIN
4 NULL;
5 END;
6 /
Procedure created.


2.如何执行

SQL> EXECUTE xs_proc;
PL/SQL procedure successfully completed.

或者执行

SQL> BEGIN
2 xs_proc;
3 END;
4 /

PL/SQL procedure successfully completed.

================================

3.存储过程显示一些信息

SQL> CREATE OR REPLACE PROCEDURE xs_proc
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE('hello');
5 END;
6 /

SQL> EXECUTE xs_proc;

要设置为ON , 才会把 hello 显示出来
SQL> SET SERVEROUTPUT ON

SQL> EXECUTE xs_proc;

==================================================

CREATE TABLE xue_sheng( id integer, xing_ming varchar(25), yu_wen number,shu_xue number);

INSERT INTO xue_sheng VALUES(1,'ZhanSan',80,90);

INSERT INTO xue_sheng VALUES(2,'LiSi',85,87);

==================================================

4.只带一个输入参数 ,把查询的结果显示出来

SQL> CREATE OR REPLACE PROCEDURE xs_proc(temp_id IN integer)
2 IS
3 name varchar2(25);
4 BEGIN
5 select xing_ming into name from xue_sheng where id=temp_id;
6 DBMS_OUTPUT.PUT_LINE(name);
7 END;
8 /

SQL> execute xs_proc(1);
ZhanSan

------------------

当输入学生的名字, 就会把他的总分(语文+数学)显示出来。

CREATE OR REPLACE PROCEDURE xs_proc(temp_name IN varchar2 )
IS
num_1 number;
num_2 number;
BEGIN
select yu_wen,shu_xue into num_1,num_2 from xue_sheng where xing_ming=temp_name;
DBMS_OUTPUT.PUT_LINE(num_1 + num_2);
END;
/


SQL> EXECUTE xs_proc('ZhanSan');
170


====================================================

5.输入参数和输出参数一起使用

SQL> CREATE OR REPLACE PROCEDURE xs_proc(temp_name IN varchar2,temp_num OUT number )
2 IS
3 num_1 number;
4 num_2 number;
5 BEGIN
6 select yu_wen,shu_xue into num_1,num_2 from xue_sheng where xing_ming=temp_name;
7 temp_num := num_1 + num_2;
8 END;
9 /

Procedure created.


SQL> DECLARE
2 tname varchar2(25);
3 tnum number;
4 BEGIN
5 tname:='ZhanSan';
6 xs_proc( tname,tnum );
7 DBMS_OUTPUT.PUT_LINE( tnum );
8 END;
9 /
170

PL/SQL procedure successfully completed.

=============================================

6.维护存储过程

  1、查看过程状态

    SELECT object_name,status FROM USER_OBJECTS WHERE object_type='PROCEDURE';

  2、重新编译过程

    ALTER PROCEDURE xs_proc COMPILE;

  3、查看过程的源代码

    SELECT * FROM USER_SOURCE WHERE TYPE='PROCEDURE';

  4、删除存储过程

    DROP PROCEDURE xs_proc;

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template