[oracle]一个最简单的oracle存储过程"proc_helloworld"

时间:2024-01-01 20:35:54

1.编写。编写一个最最简单的存储过程,给它起个名字叫做proc_helloworld
CREATE OR REPLACE PROCEDURE proc_helloworld
IS
BEGIN
   DBMS_OUTPUT.put_line ('Hello World!');
END;
/

2.创建。在sqlplus命令行界面创建该存储过程
sys@ora10g> conn sec/sec
Connected.
sec@ora10g> CREATE OR REPLACE PROCEDURE proc_helloworld
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.put_line ('Hello World!');
  5  END;
  6  /

Procedure created.

3.运行。两种方法运行存储过程
1).需要在屏幕上显示出"DBMS_OUTPUT.put_line"的输出字符串,需要做一个小小的设置
sec@ora10g> show serveroutput
serveroutput OFF
sec@ora10g> set serveroutput on
sec@ora10g> show serveroutput
serveroutput ON SIZE 10000 FORMAT WORD_WRAPPED

2).在sqlplus命令行界面使用"EXECUTE"命令(简写做"EXEC")执行
sec@ora10g> exec proc_helloworld
Hello World!

PL/SQL procedure successfully completed.

3).在一个无名PL/SQL块内运行存储过程
BEGIN
   proc_helloworld;
END;
/

sec@ora10g> BEGIN
  2     proc_helloworld;
  3  END;
  4  /
Hello World!

PL/SQL procedure successfully completed.

4.修改。修改一个存储过程只需要将修改好的存储过程在sqlplus界面先重新执行一下即可,因为在创建过程中使用的是"CREATE OR REPLACE PROCEDURE",也就是说如果没有就创建,如果已经存在了这个存储过程,就替换它
CREATE OR REPLACE PROCEDURE proc_helloworld
IS
BEGIN
   DBMS_OUTPUT.put_line ('Hello World! '||chr(10)||'I am a Happy DBA Secooler!');
END;
/
或者
CREATE OR REPLACE PROCEDURE proc_helloworld
IS
BEGIN
   DBMS_OUTPUT.put_line ('Hello World! 
I am a Happy DBA Secooler!');
END;
/

sec@ora10g> CREATE OR REPLACE PROCEDURE proc_helloworld
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.put_line ('Hello World! '||chr(10)||'I am a Happy DBA Secooler!');
  5  END;
  6  /

Procedure created.

看一下执行效果:
sec@ora10g> exec proc_helloworld;
Hello World!
I am a Happy DBA Secooler!

PL/SQL procedure successfully completed.

5.调试。对于非常复杂的存储过程的调试是真正体现个人魅力和能力的地方,往往需要很多的经验,这个急不得,只能慢慢来 Take it easy.
在sqlplus下调试存储过程时,如果出现错误,时刻使用"show errors"命令查看哪里出错了,例如:
sec@ora10g> CREATE OR REPLACE PROCEDURE proc_helloworld
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.put_line ('Hello World!');
  5  END
  6  /

Warning: Procedure created with compilation errors.

sec@ora10g> show errors
Errors for PROCEDURE PROC_HELLOWORLD:

LINE/COL ERROR
-------- -----------------------------------------------------------------
5/3      PLS-00103: Encountered the symbol "end-of-file" when expecting
         one of the following:
         ; <an identifier> <a double-quoted delimited-identifier>
         delete exists prior <a single-quoted SQL string>
         The symbol ";" was substituted for "end-of-file" to continue.

通过提示,问题出现在END后面没有分号结束符号,修改后问题得到处理
sec@ora10g> CREATE OR REPLACE PROCEDURE proc_helloworld
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.put_line ('Hello World!');
  5  END;
  6  /

Procedure created.

6.删除。使用drop语句删除存储过程
sec@ora10g> select object_name,object_type,status from user_objects where OBJECT_TYPE='PROCEDURE';

OBJECT_NAME                    OBJECT_TYPE         STATUS
------------------------------ ------------------- -------
PROC_HELLOWORLD                PROCEDURE           VALID

sec@ora10g> DROP PROCEDURE proc_helloworld;

Procedure dropped.

sec@ora10g> select object_name,object_type,status from user_objects where OBJECT_TYPE='PROCEDURE';

no rows selected

7.获取。在维护存储过程的过程中往往需要快速的获取存储过程的SQL创建语句,我经常使用的有如下两种方法
1).使用DBMS_METADATA包获得
sec@ora10g> SELECT DBMS_METADATA.get_ddl ('PROCEDURE', 'PROC_HELLOWORLD') from dual;

DBMS_METADATA.GET_DDL('PROCEDURE','PROC_HELLOWORLD')
--------------------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE "SEC"."PROC_HELLOWORLD"
IS
BEGIN
   DBMS_OUTPUT.put_line ('Hello World!');
END;

2).使用"USER_SOURCE"视图获得,本人推荐使用这种方法查看,格式标准,内容也全面
sec@ora10g> SELECT text FROM user_source WHERE NAME = 'PROC_HELLOWORLD';

TEXT
------------------------------------------------------------------------------------------------------------------------------
PROCEDURE proc_helloworld
IS
BEGIN
   DBMS_OUTPUT.put_line ('Hello World!');
END;

8.小结
上面的实验,我通过编写、创建、运行、修改、调试、删除和获取七个内容描述了一个存储过程的开发和维护过程。
试验,实验还有实践,技术工作者永远不变的途径!
Goodluck Friends!

-- The End --

http://blog.itpub.net/519536/viewspace-571504