在mysql中获取OUT参数

时间:2018-01-10 08:01:06

标签: mysql sql

DELIMITER //
CREATE PROCEDURE InsertUser
(
    IN RoleID INT,
    IN UserEmail VARCHAR(100),
    IN UserPassword VARCHAR(250),
    OUT ID INT
)
BEGIN
INSERT INTO useraccount
(
    RoleID,
    UserEmail,
    UserPassword,
    CreateDate
)
VALUES
(
    RoleID,
    UserEmail,
    UserPassword,
    CURRENT_TIMESTAMP
);
SET ID = (SELECT UserID FROM useraccount WHERE UserEmail = UserEmail);
END //
DELIMITER ;

enter image description here

我想获取OUT参数ID但是当我执行此过程时,我收到错误消息“Subquery返回超过1行”你能告诉我我的错误吗? 谢谢

1 个答案:

答案 0 :(得分:0)

你的子查询在这里是错误的:

     SELECT UserID FROM useraccount WHERE UserEmail = UserEmail

在这里,您将UserEmail与其自身进行比较,对于表中的每一行而言,这将是真的,而不是空值。

我建议更好的方法是更改​​UserEmail IN参数的名称(假设它发送电子邮件):

DELIMITER //
CREATE PROCEDURE InsertUser
(
    IN RoleID INT,
    IN email VARCHAR(100),
    IN UserPassword VARCHAR(250),
    OUT ID INT
)
BEGIN
INSERT INTO useraccount
(
    RoleID,
    UserEmail,
    UserPassword,
    CreateDate
)
VALUES
(
    RoleID,
    email ,
    UserPassword,
    CURRENT_TIMESTAMP
);
SET ID = (SELECT UserID FROM useraccount WHERE UserEmail = email);
END //
DELIMITER ;

请试试这个。希望您确保2个useraccounts没有相同的电子邮件