创建时的基本定义与调用
注:
CREATE OR REPLACE
没有就创建,有的话就更新。
SQL> CREATE OR REPLACE PROCEDURE HelloWorld AS
2 BEGIN
3 dbms_output.put_line(‘Hello World‘);
4 END HelloWorld;
5 /
Procedure created.
SQL> set serveroutput on
SQL>
SQL> BEGIN
2 HelloWorld();
3 END;
4 /
Hello World
PL/SQL procedure successfully completed.
修改存储过程
SQL> CREATE OR REPLACE PROCEDURE HelloWorld AS
2 BEGIN
3 dbms_output.put_line(‘Hello World V2.0‘);
4 END HelloWorld;
5 /
Procedure created.
SQL>
SQL> set serveroutput on
SQL>
SQL> BEGIN
2 HelloWorld();
3 END;
4 /
Hello World V2.0
PL/SQL procedure successfully completed.
参数定义
单个参数
SQL> CREATE OR REPLACE PROCEDURE HelloWorld1 (
2 p_user_name VARCHAR2
3 ) AS
4 BEGIN
5 dbms_output.put_line(‘Hello ‘ || p_user_name || ‘!‘);
6 END HelloWorld1;
7 /
Procedure created.
SQL>
SQL>
SQL> BEGIN
2 HelloWorld1(‘Edward‘);
3 END;
4 /
Hello Edward!
PL/SQL procedure successfully completed.
IN、OUT、IN OUT
SQL> CREATE OR REPLACE PROCEDURE HelloWorld2 (
2 p_user_name IN VARCHAR2,
3 p_out_val OUT VARCHAR2,
4 p_inout_val IN OUT VARCHAR2
5 ) AS
6 BEGIN
7 dbms_output.put_line(‘Hello ‘ || p_user_name || p_inout_val || ‘!‘);
8 p_out_val := ‘A‘;
9 p_inout_val := ‘B‘;
10 END HelloWorld2;
11 /
Procedure created.
SQL> DECLARE
2 p_outval VARCHAR2(10);
3 p_inoutval VARCHAR2(10) := ‘~Hi~‘;
4 BEGIN
5 HelloWorld2(‘Edward‘, p_outval, p_inoutval);
6
7 dbms_output.put_line(‘p_outval=‘ || p_outval);
8 dbms_output.put_line(‘p_inoutval=‘ || p_inoutval);
9 END;
10 /
Hello Edward~Hi~!
p_outval=A
p_inoutval=B
PL/SQL procedure successfully completed.
SQL>
参数的默认值
SQL> CREATE OR REPLACE PROCEDURE HelloWorld3 (
2 p_user_name VARCHAR2,
3 p_val1 VARCHAR2 DEFAULT ‘ Good Moning,‘,
4 p_val2 VARCHAR2 DEFAULT ‘ Nice to Meet you‘
5 ) AS
6 BEGIN
7 dbms_output.put_line(‘Hello ‘ || p_user_name || p_val1 || p_val2 || ‘!‘
);
8 END HelloWorld3;
9 /
Procedure created.
SQL> BEGIN
2 HelloWorld3(‘Edward‘);
3 HelloWorld3(‘Edward‘, ‘ Good Night,‘);
4 HelloWorld3(‘Edward‘, ‘ Good Night,‘, ‘Bye‘);
5 END;
6 /
Hello Edward Good Moning, Nice to Meet you!
Hello Edward Good Night, Nice to Meet you!
Hello Edward Good Night,Bye!
PL/SQL procedure successfully completed.
指定参数名称调用
此部分使用 “参数默认值”那一小节的存储过程。
用于说明当最后2个参数是有默认的时候,如何跳过中间那个。
SQL> BEGIN
2 HelloWorld3(‘Edward‘);
3 HelloWorld3(‘Edward‘, p_val1 => ‘ Good Night,‘);
4 HelloWorld3(‘Edward‘, p_val1 => ‘ Good Night,‘, p_val2 => ‘Bye‘);
5 HelloWorld3(‘Edward‘, p_val2 => ‘ HeiHei ‘);
6 END;
7 /
Hello Edward Good Moning, Nice to Meet you!
Hello Edward Good Night, Nice to Meet you!
Hello Edward Good Night,Bye!
Hello Edward Good Moning, HeiHei !
PL/SQL procedure successfully completed.
存储过程返回结果集
有些人原先是在SQL Server下的存储过程的。
SQL Server的存储过程,可以在里面写一段SQL语句,然后其它的开发语言比如C#,可以在调用存储过程的后,获取到存储过程执行所返回的结果集。
此方法在 SQL Server 中可用,在 Oracle 中不可用。
首先举一个 SQL Server 的成功的例子:
CREATEPROCEDURE testProc
AS
BEGIN
SELECT‘Hello 1‘AS A,‘World 1‘AS B UNIONALL
SELECT‘Hello 2‘AS A,‘World 2‘AS B;
END
BEGIN
DECLARE @RC int;
EXECUTE @RC = testProc;
PRINT @RC;
END
A B
------- -------
Hello 1 World 1
Hello 2 World 2
(2 行受影响)
0
然后 照着写一个 Oracle 的。
SQL> CREATE OR REPLACE PROCEDURE testProc
2 AS
3 BEGIN
4 SELECT ‘Hello 1‘ AS A, ‘World 1‘ AS B FROM DUAL UNION ALL
5 SELECT ‘Hello 2‘ AS A, ‘World 2‘ AS B FROM DUAL ;
6 END;
7 /
警告:创建的过程带有编译错误。
SQL> show err;
PROCEDURE TESTPROC出现错误:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/3 PLS-00428: 在此 SELECT 语句中缺少 INTO 子句
假如要实现 存储过程返回结果集,以便让 C# 之类的代码调用的话,请参考 Oracle 函数 当中的 返回结果集的函数的部分。