MySQL使用分隔符创build存储过程语法

我正在尝试在MySQL中使用如下分隔符创build一个存储过程:

use am; DELIMITER $$ CREATE PROCEDURE addfields() BEGIN DECLARE done INT DEFAULT FALSE; DECLARE acc INT(16); DECLARE validId INT DEFAULT 0; END $$ DELIMITER ; 

它给了我一个错误:

 #1304 - PROCEDURE addfields already exists 

使用分隔符创build存储过程的恰当语法是什么,如果它首先存在,则将其删除?

以下是带分隔符的示例MYSQL存储过程以及如何调用..

 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_user_login` $$ CREATE DEFINER=`root`@`%` PROCEDURE `sp_user_login`( IN loc_username VARCHAR(255), IN loc_password VARCHAR(255) ) BEGIN SELECT user_id, user_name, user_emailid, user_profileimage, last_update FROM tbl_user WHERE user_name = loc_username AND password = loc_password AND status = 1; END $$ DELIMITER ; 

并通过调用mysql_connection规范和

 $loginCheck="call sp_user_login('".$username."','".$password."');"; 

它会返回程序的结果。

在MySQL中使用存储过程语法入门:

好的程序员使用terminal,GUI让你在中间变得柔软。 当你习惯并记住命令时,它比任何GUI快5倍。 生产力=成功。

1.打开一个terminal,像这样login到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) 

好吧,现在我没有定义存储过程。 做最简单的一个:

 mysql> delimiter // mysql> create procedure foobar() -> begin select 'hello'; end// Query OK, 0 rows affected (0.00 sec) 

当您input存储过程的命令时,//将与terminal通信。 存储的过程名称是foobar。 它没有参数,应该返回“你好”。

看看它是否在那里,记得把你的分隔符放回去!

  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) 

太好了! 我们做了一个input,修改和输出的程序。 现在让我们做一个variables。

9.删除foobar,创build一个outvariables,运行它:

 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) 

好吧,它的工作,它join了string在一起。 所以你定义了一个variablesmsg,并将该variables传入存储过程foobar中,@ msg被foobar写入。

现在您知道如何使用分隔符创build存储过程。 继续本教程,从存储过程中的variables开始: http : //net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/

这里是我在MySQL中创build过程的代码:

 DELIMITER $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `procedureName`(IN comId int) BEGIN select * from tableName (add joins OR sub query as per your requirement) Where (where condition here) END $$ DELIMITER ; 

要调用此过程,请使用以下查询:

 call procedureName(); // without parameter call procedureName(id,pid); // with parameter 

详情:

1)DEFINER:root是用户名,根据你的用户名mysql来更改它localhost是主机,如果你在主机服务器上执行这个查询,你可以用服务器的IP地址改变它。

在这里阅读更多的细节