2016-04-26 9 views
0
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 ; 

これは私が取り組む手順です。新しいパブリッシャを作成することを目的としています。新しいパブリッシャが作成されたかどうかにかかわらず、OUTパラメータであるisSuccess変数によって知られています。発行者が作成されていても、常に0になります。MySQLから正しい出力を得ることができません手順

私はMySQL WorkBenchで動作するMySQL 5.7.12を使用しています。 ライン以下を経由して手順を確認する:MySQLコマンドラインから

CALL create_Publisher('U_GUID20','[email protected]','Hahaha','P_GUID20','ThePublisher',NULL, @isSucess); 

SELECT @isSuccess; 

答えて

1

試してみてください。あなたのストアドプロシージャで

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', 
    -> '[email protected]', 
    -> '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 | [email protected] | 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', 
    -> '[email protected]', 
    -> '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 | [email protected] | 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` 
); 
. 
. 
. 
+0

ことができますしてくださいエラーを指摘しますか? WorkBenchによるエラーですか? – gagangupt16

+0

@ gagangupt16:どのバージョンのMySQL Workbenchを使用していますか?私はMySQL Workbench 6.3.6を使用しており、問題を再現できません。 – wchiquito

+0

私はWorkBench 6.0.8を使用しています – gagangupt16

関連する問題