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,创建一个 out 变量,运行它:
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/