无法从MySQL过程获得正确的输出

时间:2016-04-26 09:33:56

标签: mysql stored-procedures transactions

DELIMITER $$

CREATE PROCEDURE `create_Publisher`(IN userGuid VARCHAR(36), IN pubEmail VARCHAR(120), IN passwd VARCHAR(120), IN pubGuid VARCHAR(36), IN pubName VARCHAR(120), IN cat VARCHAR(500), OUT isSuccess TINYINT)
BEGIN

    DECLARE `_rollback` BOOL DEFAULT 0;
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
    SET isSuccess = 1;
    START TRANSACTION;

    INSERT INTO users (guid,email,password,role) VALUES (userGuid,pubEmail,passwd,'publisher');
    SELECT @userid := id FROM users WHERE email=pubEmail;
    INSERT INTO publisher (guid,name,cat,status,user_id) VALUES (pubGuid,pubName,cat,'pause',@userid);

    IF `_rollback` THEN
        SET isSuccess = 0;
        ROLLBACK;
    ELSE
        SET isSuccess = 1;
        COMMIT;
    END IF;
END$$

DELIMITER ;

这是我正在处理的程序。目的是创建一个新的Publisher。是否创建了新的发布者,isSuccess变量是一个OUT参数。即使正在创建发布者,它总是给我0。

我正在使用MySQL 5.7.12,正在使用MySQL WorkBench。 通过以下行检查程序:

CALL create_Publisher('U_GUID20','r@r.com','Hahaha','P_GUID20','ThePublisher',NULL, @isSucess);

SELECT @isSuccess;

1 个答案:

答案 0 :(得分:1)

尝试使用MySQL命令行:

mysql> SELECT VERSION()//
+-----------+
| VERSION() |
+-----------+
| 5.7.12    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `users`//
Query OK, 0 rows affected (0.01 sec)

mysql> DROP TABLE IF EXISTS `publisher`//
Query OK, 0 rows affected (0.00 sec)

mysql> DROP PROCEDURE IF EXISTS `create_Publisher`//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `users` (
    ->   `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->   `guid` VARCHAR(36) NOT NULL,
    ->   `email` VARCHAR(120),
    ->   `password` VARCHAR(120),
    ->   `role` VARCHAR(12)
    -> )//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `publisher` (
    ->   `guid` VARCHAR(36) NOT NULL,
    ->   `name` VARCHAR(120),
    ->   `cat` VARCHAR(500),
    ->   `status` VARCHAR(12),
    ->   `user_id` VARCHAR(12)
    -> )//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE `create_Publisher`(
    ->   `userGuid` VARCHAR(36),
    ->   `pubEmail` VARCHAR(120),
    ->   `passwd` VARCHAR(120),
    ->   `pubGuid` VARCHAR(36),
    ->   `pubName` VARCHAR(120),
    ->   `cat` VARCHAR(500),
    ->   OUT `isSuccess` TINYINT
    -> )
    -> BEGIN
    ->   DECLARE `_rollback` BOOL DEFAULT 0;
    ->   DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` := 1;
    ->   SET `isSuccess` := 1;
    ->   START TRANSACTION;
    -> 
    ->   INSERT INTO `users` (
    ->     `guid`,
    ->     `email`,
    ->     `password`,
    ->     `role`
    ->   ) VALUES (
    ->     `userGuid`,
    ->     `pubEmail`,
    ->     `passwd`,
    ->     'publisher'
    ->   );
    ->   SELECT @`userid` := `id`
    ->   FROM `users`
    ->   WHERE `email` = `pubEmail`;
    -> 
    ->   INSERT INTO `publisher` (
    ->     `guid`,
    ->     `name`,
    ->     `cat`,
    ->     `status`,
    ->     `user_id`
    ->   ) VALUES (
    ->     `pubGuid`,
    ->     `pubName`,
    ->     `cat`,
    ->     'pause',
    ->     @`userid`
    ->   );
    -> 
    ->   IF `_rollback` THEN
    ->     SET `isSuccess` := 0;
    ->       ROLLBACK;
    ->   ELSE
    ->     SET `isSuccess` := 1;
    ->     COMMIT;
    ->   END IF;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `create_Publisher`(
    ->   'U_GUID20',
    ->   'r@r.com',
    ->   'Hahaha',
    ->   'P_GUID20',
    ->   'ThePublisher',
    ->   NULL,
    ->   @`isSucess`
    -> );
+-------------------+
| @`userid` := `id` |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> SELECT @`isSucess`;
+-------------+
| @`isSucess` |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `guid`,
    ->   `email`,
    ->   `password`,
    ->   `role`
    -> FROM
    ->   `users`;
+----------+---------+----------+-----------+
| guid     | email   | password | role      |
+----------+---------+----------+-----------+
| U_GUID20 | r@r.com | Hahaha   | publisher |
+----------+---------+----------+-----------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `guid`,
    ->   `name`,
    ->   `cat`,
    ->   `status`,
    ->   `user_id`
    -> FROM
    ->   `publisher`;
+----------+--------------+------+--------+---------+
| guid     | name         | cat  | status | user_id |
+----------+--------------+------+--------+---------+
| P_GUID20 | ThePublisher | NULL | pause  | 1       |
+----------+--------------+------+--------+---------+
1 row in set (0.00 sec)

mysql> -- FAIL
mysql> CALL `create_Publisher`(
    ->   'U_GUID21',
    ->   'r1@r.com',
    ->   'Hahaha1',
    ->   NULL,           -- <<<< SQLEXCEPTION >>>>
    ->   'ThePublisher1',
    ->   NULL,
    ->   @`isSucess`
    -> );
+-------------------+
| @`userid` := `id` |
+-------------------+
|                 2 |
+-------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @`isSucess`;
+-------------+
| @`isSucess` |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `guid`,
    ->   `email`,
    ->   `password`,
    ->   `role`
    -> FROM
    ->   `users`;
+----------+---------+----------+-----------+
| guid     | email   | password | role      |
+----------+---------+----------+-----------+
| U_GUID20 | r@r.com | Hahaha   | publisher |
+----------+---------+----------+-----------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `guid`,
    ->   `name`,
    ->   `cat`,
    ->   `status`,
    ->   `user_id`
    -> FROM
    ->   `publisher`;
+----------+--------------+------+--------+---------+
| guid     | name         | cat  | status | user_id |
+----------+--------------+------+--------+---------+
| P_GUID20 | ThePublisher | NULL | pause  | 1       |
+----------+--------------+------+--------+---------+
1 row in set (0.00 sec)

在存储过程中,您可以更改以下内容:

.
.
.
DECLARE `userid` INT UNSIGNED;
.
.
.

/*
SELECT @`userid` := `id`
FROM `users`
WHERE `email` = `pubEmail`;
*/
SET `userid` := LAST_INSERT_ID();

INSERT INTO `publisher` (
  `guid`,
  `name`,
  `cat`,
  `status`,
  `user_id`
) VALUES (
  `pubGuid`,
  `pubName`,
  `cat`,
  'pause',
  `userid`
);
.
.
.