mysql - mysql存储过程逻辑有问题

标签 mysql

例如,我在 checkfirstnamecount!=1 上遇到逻辑问题:

DELIMITER go
    /* Passing the parameters */
    /*  I still get firstname error message every time I tried to execute*/
    Create procedure registerusers(
      Out UserID tinyint(11),
     IN iFirstName varchar(30),
     IN iLastName varchar(30),
     IN iPassword varchar(30),
     IN iEmailAddress varchar(30),
     IN iSalt varchar(40),
     IN iRoleID varchar(1))
    BEGIN
    /* declaring thecheckfirstnamecount for counting first name*/
    declare checkfirstnamecount int;
    select count(FirstName) into checkfirstnamecount
    from users
    where FirstName = iFirstName;
    /*checking if the count firstname is not equal to 1 */
     /* if count 1 is not equal to count 1 then it should display the message*/   
    If(checkfirstnamecount!=1) then

    SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = 'Fill out the First Name ';

    else

    insert into users(
        /* insert into user if its not empty */
    FirstName,
     LastName ,
     Password ,
     EmailAddress ,
     Salt ,
     RoleID 
     )
    Values
    (
    iFirstName,
     iLastName ,
     iPassword ,
     iEmailAddress ,
     iSalt ,
     iRoleID 
    );
    set UserID = last_insert_id();  
     end if;

    End
    go
    DELIMITER ;

结果是它确实执行,但每当我传递时

call registerusers(@new_id,'Jerry','Johnson','5566','johnson@gmail.com','1243','U');

我一直看到相同的显示消息:

fill out the first name

它假设执行插入数据。

最佳答案

检查您的过程代码,如下所示。因此,名字 Jerry 匹配多个记录,从而匹配最终结果。

If(checkfirstnamecount!=1) then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Fill out the First Name ';

您的局部变量 checkfirstnamecount 由以下查询填充

select count(FirstName) into checkfirstnamecount
from users
where FirstName = iFirstName;

我觉得,您的意思是检查是否不存在该名字的记录,然后插入此记录,在这种情况下,您的情况实际上应该是

If(checkfirstnamecount <> 0) then
  //PRINT error message
ELSE
   //perform insertion

关于mysql - mysql存储过程逻辑有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455900/

相关文章:

php - 进行简单的搜索

mysql - 在sql中返回日期范围内的项目

mysql - R-连接到远程mysql服务器

mysql - 从多个表中选择值 - SQL

php - Mysql - 查询 GroupBy 未返回正确的时间

更改 my.cf 后 MySQL 将无法启动

mysql - 如何仅使用指定文本后的子字符串更新新列 - MySQL

mysql - Laravel 和 MySql 中的多重搜索问题

php - 地名城市 [邮政编码] 表

mysql - 使用 vb 2012 从 mysql 获取特定的行 col 值