您的位置:首页 > 编程语言 > Java开发

java 常用数据库存储过程的创建示例

2012-07-13 13:06 399 查看
1.MySQL 数据库的procedure 相关用法说明:

原文出处:http://hi.baidu.com/fegro/item/41344414cc3fdad6bf904291


MySQL 创建存储过程用法,CREATE PROCEDURE的用法详解!

我们大家都知道MySQL 存储过程是从 MySQL 5.0 开始逐渐增加新的功能。存储过程在实际应用中也是优点大于缺点。不过最主要的还是执行效率和SQL 代码封装。特别是 SQL 代码封装功能,如果没有存储过程。

在外部程序访问数据库时(例如 PHP),要组织很多 SQL 语句。

特别是业务逻辑复杂的时候,一大堆的 SQL 和条件夹杂在 PHP 代码中,让人不寒而栗。现在有了 MySQL 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高。

一、MySQL 创建存储过程

“pr_add” 是个简单的 MySQL 存储过程,这个MySQL 存储过程有两个 int 类型的输入参数 “a”、“b”,返回这两个参数的和。
drop procedure if exists pr_add;

计算两个数之和
create procedure pr_add ( a int, b int ) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c as sum; /* return c;

不能在 MySQL 存储过程中使用。return 只能出现在函数中。
*/ end;

二、调用 MySQL 存储过程
call pr_add(10, 20);

执行 MySQL 存储过程,存储过程参数为 MySQL 用户变量。
set @a = 10; set @b = 20; call pr_add(@a, @b);

三、MySQL 存储过程特点

创建 MySQL 存储过程的简单语法为:
create procedure 存储过程名字() ( [in|out|inout] 参数 datatype ) begin MySQL 语句; end;

MySQL 存储过程参数如果不显式指定“in”、“out”、“inout”,则默认为“in”。习惯上,对于是“in” 的参数,我们都不会显式指定。

1. MySQL 存储过程名字后面的“()”是必须的,即使没有一个参数,也需要“()”

2. MySQL 存储过程参数,不能在参数名称前加“@”,如:“@a int”。下面的创建存储过程语法在 MySQL 中是错误的(在 SQL Server 中是正确的)。 MySQL 存储过程中的变量,不需要在变量名字前加“@”,虽然 MySQL 客户端用户变量要加个“@”。
create procedure pr_add ( @a int, -- 错误 b int -- 正确 )

3. MySQL 存储过程的参数不能指定默认值。

4. MySQL 存储过程不需要在 procedure body 前面加 “as”。而 SQL Server 存储过程必须加 “as” 关键字。
create procedure pr_add ( a int, b int ) as -- 错误,MySQL 不需要 “as” begin mysql statement ...; end;

5. 如果 MySQL 存储过程中包含多条 MySQL 语句,则需要 begin end 关键字。
create procedure pr_add ( a int, b int ) begin mysql statement 1 ...; mysql statement 2 ...; end;

6. MySQL 存储过程中的每条语句的末尾,都要加上分号 “;”
... declare c int; if a is null then set a = 0; end if; ... end;

7. MySQL 存储过程中的注释。
/* 这是个 多行 MySQL 注释。 */ declare c int; -- 这是单行 MySQL 注释 (注意 -- 后至少要有一个空格) if a is null then # 这也是个单行 MySQL 注释 set a = 0; end if; ... end;

8. 不能在 MySQL 存储过程中使用 “return” 关键字。
set c = a + b; select c as sum; /* return c; -- 不能在 MySQL 存储过程中使用。return 只能出现在函数中。 */ end;

9. 调用 MySQL 存储过程时候,需要在过程名字后面加“()”,即使没有一个参数,也需要“()”
call pr_no_param();

10. 因为 MySQL 存储过程参数没有默认值,所以在调用 MySQL 存储过程时候,不能省略参数。可以用 null 来替代。

下面本人亲自实践的代码:

mysql> create procedure p_add(a int,b int)

-> begin

-> declare c int;

-> if a is null then

-> set a = 0;

-> end if;

-> if b is null then

-> set b = 0;

-> end if;

-> set c = a + b;

-> select c as sum;

-> return c;

-> //

ERROR 1313 (42000): RETURN is only allowed in a FUNCTION

mysql> create procedure p_add(a int,b int) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c as sum; return c;end;

-> //

ERROR 1313 (42000): RETURN is only allowed in a FUNCTION

mysql> delimiter //

mysql> create procedure p_add(a int,b int) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c as sum; return c;end;

-> //

ERROR 1313 (42000): RETURN is only allowed in a FUNCTION

mysql> create procedure p_add(a int,b int) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c as sum;end;

-> //

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> call p_add(10,20);

+------+

| sum |

+------+

| 30 |

+------+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> set @a = 10;

Query OK, 0 rows affected (0.00 sec)

mysql> set @b = 40;

Query OK, 0 rows affected (0.00 sec)

mysql> call p_add(@a,@b);

+------+

| sum |

+------+

| 50 |

+------+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

//建个临时表,用存储过程插入50万条数据,哎用了33秒钟才搞定!

createtable t_06 (

id intnotnullprimarykey,

c1 varchar(30),

i2 int

) engine = myisam;

delimiter //

CREATEPROCEDURE prepareData_t_06 ()

BEGIN

DECLARE i INTDEFAULT1;

WHILE i <500000 DO

insertinto t_06 values (i,concat('AA',i),i);

SET i = i +1;

ENDWHILE;

END;

//

delimiter ;

CALL prepareData_t_06();

selectcount(*) from t_06;

show indexfrom t_06;

show table status like't_06';

Oracle 的 procedure 的创建:

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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: