1

我有一个存储过程,它检查是否存在与输入具有相同电子邮件地址的用户,如果没有,则注册一个。

这是表格:

CREATE  TABLE IF NOT EXISTS `overkill`.`accounts` (
  `accountID` INT NOT NULL AUTO_INCREMENT ,
  `email` VARCHAR(64) NOT NULL ,
  `firstName` VARCHAR(32) NOT NULL ,
  `lastName` VARCHAR(32) NOT NULL ,
  `passSaltedSHA` BINARY(20) NOT NULL ,
  `salt` BINARY(20) NOT NULL ,
  `gender` ENUM('m','f') NOT NULL ,
  `birthDate` DATE NOT NULL ,
  `regTime` TIMESTAMP NOT NULL ,
  PRIMARY KEY (`accountID`) )
ENGINE = InnoDB;

这是存储过程:

DELIMITER $$

CREATE PROCEDURE `overkill`.`registerUser` (
    IN emailIN VARCHAR(64),
    IN passwordIN VARCHAR(16),
    IN firstNameIN VARCHAR(32),
    IN lastNameIn VARCHAR(32),
    IN birthIN DATE,
    IN genderIN ENUM('f','m'))

BEGIN
    DECLARE existingMailAccLOG INT DEFAULT NULL;
    DECLARE saltLOC CHAR(40);
    DECLARE regSuccessLOC BOOLEAN DEFAULT FALSE;

    SELECT COUNT(*) INTO existingMailAccLOG FROM `overkill`.`accounts` WHERE `accounts`.`email` = emailIN;

    IF existingMailAccLOG = 0 THEN
        SET saltLOC = SHA1(rand());
        SET regSuccessLOC = TRUE;
        INSERT INTO `overkill`.`accounts` (`email`, `firstName`, `lastName`, `passSaltedSHA`, `salt`, `gender`, `birthDate`) VALUES(emailIN, firstNameIN, lastNameIn, UNHEX(SHA1(CONCAT(passwordIN, saltLOC))), UNHEX(saltLOC), genderIN, birthIN);
    END IF;

    SELECT regSuccessLOC AS `registered`, saltLOC AS `salt`;
END

如果我打电话:

CALL registerUser("abc@def.com", "pass", "firstn", "lastn", "2012-01-01", "f");

它在表中插入一行accounts,但忘记返回我在 IF 中设置的正确值

SET saltLOC = SHA1(rand());
SET regSuccessLOC = TRUE;

怎么可能?为什么这些行被跳过并且 INSERT 仍然被执行,没有错误?

4

1 回答 1

0

尝试在DECLARE关键字后的变量名前添加“@”。它可能会引起一些混乱,如下所述:MySQL:@variable vs. variable。有什么不同?(第 2 部分)和这里:MySQL:@variable 与变量。有什么不同?

于 2013-05-05T18:38:59.870 回答