簡體   English   中英

MySQL 創建帶分隔符的存儲過程語法

[英]MySQL create stored procedure syntax with delimiter

我正在嘗試使用如下分隔符在 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。 它不需要參數,應該返回“你好”。

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/

這是帶有分隔符的示例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 中創建過程的代碼:

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

閱讀此處了解更多詳情

我創建了一個簡單的 MySQL 程序,如下所示:

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

請按照這個。 創建程序后,您可以看到相同的內容並執行它。

我的 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