40

我正在尝试使用这样的分隔符在 MySQL 中创建一个存储过程:

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

使用分隔符创建存储过程并在它首先存在时删除它的正确语法是什么?

4

5 回答 5

74

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/

于 2013-12-06T21:00:53.150 回答
62

这是带有分隔符的示例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."');";

它将返回过程的结果。

于 2013-04-03T11:53:02.163 回答
2

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

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地址进行更改。

阅读此处了解更多详情

于 2015-08-27T06:16:08.440 回答
0

我创建了一个简单的 MySQL 程序,如下所示:

DELIMITER //
CREATE PROCEDURE GetAllListings()
 BEGIN
 SELECT nid, type, title  FROM node where type = 'lms_listing' order by nid desc;
END //
DELIMITER;

请遵循此。创建过程后,您可以看到相同的内容并执行它。

于 2018-07-19T11:58:41.647 回答
0

我的 SQL 存储过程创建

DELIMiTER $$
create procedure GetUserRolesEnabled(in UserId int)
Begin

select * from users
where id=UserId ;
END $$
DELIMITER ;
于 2019-05-29T12:43:17.780 回答