简体   繁体   English

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

[英]MySQL create stored procedure syntax with delimiter

I am trying to create a stored procedure in MySQL using a delimiter like this:我正在尝试使用如下分隔符在 MySQL 中创建一个存储过程:

use am;

DELIMITER $$

CREATE PROCEDURE addfields()
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE acc INT(16);
  DECLARE validId INT DEFAULT 0;

END $$

DELIMITER ;

It gives me an error:它给了我一个错误:

#1304 - PROCEDURE addfields already exists

What is the proper syntax for making a stored procedure with a delimiter and dropping it if it exists first?使用分隔符创建存储过程并在它首先存在时删除它的正确语法是什么?

Getting started with stored procedure syntax in MySQL (using the terminal): MySQL 中的存储过程语法入门(使用终端):

1. Open a terminal and login to mysql like this: 1.打开一个终端并像这样登录到mysql:

el@apollo:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> 

2. Take a look to see if you have any procedures: 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)

I have one defined, you probably have none to start out.我有一个定义,你可能没有一个可以开始。

3. Change to the database, delete it. 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. Ok so now I have no stored procedures defined. 4. 好的,现在我没有定义存储过程。 Make the simplest one:做一个最简单的:

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

The // will communicate to the terminal when you are done entering commands for the stored procedure.当您完成为存储过程输入命令时, // 将与终端通信。 the stored procedure name is foobar.存储过程名称是 foobar。 it takes no parameters and should return "hello".它不需要参数,应该返回“你好”。

5. See if it's there, remember to set back your delimiter!: 5.看看有没有,记得把你的分隔符调回去!:

 mysql> show procedure status;
 -> 
 -> 

Gotcha!知道了! Why didn't this work?为什么这不起作用? You set the delimiter to // remember?您将分隔符设置为//还记得吗? Set it back to ;将其重新设置为;

6. Set the delimiter back and look at the procedure: 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. Run it: 7.运行它:

mysql> call foobar();
+-------+
| hello |
+-------+
| hello |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

Hello world complete, lets overwrite it with something better.你好世界完成了,让我们用更好的东西覆盖它。

8. Drop foobar, redefine it to accept a parameter, and re run it: 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)

Nice!好的! We made a procedure that takes input, modifies it, and does output.我们制作了一个接受输入、修改它并输出的过程。 Now lets do an out variable.现在让我们做一个输出变量。

9. Remove foobar, Make an out variable, run it: 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. Example of INOUT usage in MySQL: 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)

Ok it worked, it joined the strings together.好的,它起作用了,它将字符串连接在一起。 So you defined a variable msg, passed in that variable into stored procedure called foobar, and @msg was written to by foobar.因此,您定义了一个变量 msg,将该变量传递到名为 foobar 的存储过程中,@msg 由 foobar 写入。

Now you know how to make stored procedures with delimiters.现在您知道如何使用分隔符创建存储过程了。 Continue this tutorial here, start in on variables within stored procedures: http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/在此处继续本教程,从存储过程中的变量开始: http : //net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/

Here is the sample MYSQL Stored Procedure with delimiter and how to call..这是带有分隔符的示例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 ;

and call by, mysql_connection specification and并调用,mysql_connection 规范和

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

it will return the result from the procedure.它将返回程序的结果。

Here is my code to create procedure in MySQL :这是我在 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 ;

To call this procedure use this query :要调用此过程,请使用以下查询:

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

Detail :细节 :

1) DEFINER : root is the user name and change it as per your username of mysql localhost is the host you can change it with ip address of the server if you are execute this query on hosting server. 1) DEFINER : root 是用户名,并根据您的 mysql 用户名进行更改 localhost 是主机,如果您在托管服务器上执行此查询,则可以使用服务器的 ip 地址更改它。

Read here for more detail 阅读此处了解更多详情

I have created a simple MySQL procedure as given below:我创建了一个简单的 MySQL 程序,如下所示:

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

Kindly follow this.请按照这个。 After the procedure created, you can see the same and execute it.创建程序后,您可以看到相同的内容并执行它。

MY SQL STORED PROCEDURE CREATION我的 SQL 存储过程创建

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

select * from users
where id=UserId ;
END $$
DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM