gpt4 book ai didi

MySQL创建带分隔符的存储过程语法

转载 作者:行者123 更新时间:2023-11-29 19:15:12 26 4
gpt4 key购买 nike

我正在尝试使用如下分隔符在 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

创建带有分隔符的存储过程并在它先存在时将其删除的正确语法是什么?

最佳答案

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/

关于MySQL创建带分隔符的存储过程语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42767900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com