【问题标题】:MySQL create stored procedure syntax with delimiterMySQL 创建带分隔符的存储过程语法
【发布时间】:2013-03-25 01:17:12
【问题描述】:

我正在尝试使用这样的分隔符在 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

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

【问题讨论】:

  • 请注意,您的语法很好,此错误仅表示程序已经创建(您第二次运行脚本)。先放下:DROP PROCEDURE addfields; 在执行USE 之后和执行DELIMITER 之前(因为我使用了;,如果你使用DROP PROCEDURE addfields$$,它将在DELIMITER 之后和CREATE PROCEDURE 之前)

标签: mysql sql stored-procedures


【解决方案1】:

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

它将返回过程的结果。

【讨论】:

  • 它返回什么? 它返回任何值返回真/假。
  • 我有一个问题:为什么有些语句以; 结尾,而另一些则以DELIMITER $$ 结尾。如果分隔符表示语句的结尾,为什么它们都不是$$ 甚至还不是,为什么分隔符甚至是必要的?你不能用; 结束陈述吗?好困惑。
  • 更改分隔符允许您在 proc 声明中使用标准分隔符,否则 mysql 将在那里终止。声明后,您将其改回。
  • 在mariadb上运行时需要最后一个DELIMITER语句后面的空格,不确定mysql。
  • 您也可以执行“创建或替换过程 sp_user_login”而不是先删除它。
【解决方案2】:

开始使用 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)

Hello world 完成,让我们用更好的东西覆盖它。

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/

【讨论】:

  • 这太棒了。谢谢你。这应该是维基百科的
  • 惊人的解释。非常感谢:-)
【解决方案3】:

这是我在 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 地址进行更改。

Read here了解更多详情

【讨论】:

    【解决方案4】:

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

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

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

    【讨论】:

    • 这个答案似乎没有为讨论添加任何内容,这在之前的答案中没有涉及。 稍后添加答案时,请说明您的答案显示的内容,现有答案尚未显示。
    【解决方案5】:

    我的 SQL 存储过程创建

    DELIMiTER $$
    create procedure GetUserRolesEnabled(in UserId int)
    Begin
    
    select * from users
    where id=UserId ;
    END $$
    DELIMITER ;
    

    【讨论】:

    • 这个答案似乎没有为讨论添加任何内容,这在之前的答案中没有涉及。 稍后添加答案时,请说明您的答案显示的内容,现有答案尚未显示。
    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2016-11-11
    • 2017-10-14
    • 2021-07-10
    相关资源
    最近更新 更多