网站开发技术

记点笔记、 学点技术 欢迎交流建站技术。本站关注lamp技术

您尚未登录。

#1 2014-12-06 16:08:00

admin
管理员

mysql 存储过程的一个例子

示例 

mysql> delimiter //
mysql> create procedure proc()
    -> begin
    -> select * from tb1;
    -> end //
Query OK, 0 rows affected (0.04 sec)

mysql>
mysql>
mysql> create table tb1 (name varchar(20));
    -> //
Query OK, 0 rows affected (0.27 sec)

mysql> delimiter ;
mysql> insert into tb1 values ('a'), ('b') ,('c');
Query OK, 3 rows affected (0.10 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> call proc;
+------+
| name |
+------+
| a    |
| b    |
| c    |
+------+
3 rows in set (0.14 sec)

Query OK, 0 rows affected (0.14 sec)

解释:

delimiter  -- 默认情况是 mysql 遇到 ; (分号) 解释输入的语句, 但是在存储过程中,sql语句中经常要使用 分号。

delimiter // 就表示遇到 // 才执行。 

所在遇到语句  create table tb1 ... ; 的时候,没有运行。

存储过程的格式是固定的。 

create procedurc 名字 

begin 

以上两个行开始

end 结束

调用的时候 用 call


ipbbs.net

离线

页脚

Powered by FluxBB