在mysql存储过程中遇到逻辑问题

时间:2016-09-12 17:46:14

标签: 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');

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

  

填写名字

它假设执行插入数据。

1 个答案:

答案 0 :(得分:0)

检查您的程序代码,如下所示。因此,名字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