試してみてください。あなたのストアドプロシージャで
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`
);
.
.
.
ことができますしてくださいエラーを指摘しますか? WorkBenchによるエラーですか? – gagangupt16
@ gagangupt16:どのバージョンのMySQL Workbenchを使用していますか?私はMySQL Workbench 6.3.6を使用しており、問題を再現できません。 – wchiquito
私はWorkBench 6.0.8を使用しています – gagangupt16