MySQL存储过程语法入门(使用终端):
1. 打开终端并登录mysql,如下所示:
el@apollo:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>
2.查看是否有任何程序:
mysql> show procedure status;
+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+
| yourdb | sp_user_login | PROCEDURE | root@% | 2013-12-06 14:10:25 | 2013-12-06 14:10:25 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+
1 row in set (0.01 sec)
我已经定义好了,你可能还没有开始。
3. 修改数据库之后,删除它。
mysql> use yourdb;
Database changed
mysql> drop procedure if exists sp_user_login;
Query OK, 0 rows affected (0.01 sec)
mysql> show procedure status;
Empty set (0.00 sec)
4. 好的,现在我没有定义任何存储过程。制作最简单的一个:
mysql> delimiter //
mysql> create procedure foobar()
-> begin select 'hello'; end//
Query OK, 0 rows affected (0.00 sec)
//表示您已经输入完存储过程的命令,并将结果传输到终端。存储过程名为foobar,不接受任何参数,应返回“hello”。
5. 看一下它是否存在,记得设置回分隔符!:
mysql> show procedure status;
->
->
懂了! 为什么这个不行? 你记得把分隔符设为//
了吗? 把它改回;
6. 把分隔符改回来,再看看这个过程:
mysql> delimiter ;
mysql> show procedure status;
+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+
| yourdb | foobar | PROCEDURE | root@localhost | 2013-12-06 14:27:23 | 2013-12-06 14:27:23 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+
1 row in set (0.00 sec)
7. 运行它:
mysql> call foobar();
+
| hello |
+
| hello |
+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
你好世界完成了,我们可以用更好的东西来覆盖它。
8. 删除 foobar ,重新定义它以接受一个参数并重新运行:
mysql> drop procedure foobar;
Query OK, 0 rows affected (0.00 sec)
mysql> show procedure status;
Empty set (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar (in var1 int)
-> begin select var1 + 2 as result;
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call foobar(5);
+--------+
| result |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
太好了!我们已经建立了一个接收输入、修改它并输出结果的过程。现在让我们来做一个输出变量。
9. 删除"foobar",创建一个输出变量,并运行它:
mysql> delimiter ;
mysql> drop procedure foobar;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar(out var1 varchar(100))
-> begin set var1="kowalski, what's the status of the nuclear reactor?";
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call foobar(@kowalski_status);
Query OK, 0 rows affected (0.00 sec)
mysql> select @kowalski_status;
+
| @kowalski_status |
+
| kowalski, what's the status of the nuclear reactor? |
+-----------------------------------------------------+
1 row in set (0.00 sec)
10. MySQL中INOUT用法示例:
mysql> select 'ricksays' into @msg;
Query OK, 1 row affected (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar (inout msg varchar(100))
-> begin
-> set msg = concat(@msg, " never gonna let you down");
-> end//
mysql> delimiter ;
mysql> call foobar(@msg);
Query OK, 0 rows affected (0.00 sec)
mysql> select @msg;
+
| @msg |
+
| ricksays never gonna let you down |
+
1 row in set (0.00 sec)
好的,它起作用了,它将字符串连接在一起。所以你定义了一个变量msg,把这个变量传递给存储过程foobar,而@msg被foobar写入。
现在你知道如何使用分隔符制作存储过程了。继续学习本教程,在存储过程中开始使用变量: http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/
DROP PROCEDURE addfields;
在执行USE
之后,在执行DELIMITER
之前(因为我使用了;
,如果你使用DROP PROCEDURE addfields$$
,那么它将在DELIMITER
之后和CREATE PROCEDURE
之前)。 - Xenos