如何检查mysql过程中是否不存在firstname

时间:2016-09-15 21:02:55

标签: mysql

如何检查名字是否未填写且我是否尝试过不像这样:

DELIMITER go

    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

      /* I've used not null and it works for empty firstname but when I
        try to pass the firstname it didn't work and I kept getting an error message saying "fill out the firstname" */
    If(iFirstName not null) 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 ;

然而,当我使用

set @new_id = null;
call registerusers(@new_id,'','Jones','5566','jones@gmail.com','sdfd','1');
select @new_id;

它设法显示错误消息,但是当我使用

set @new_id = null;
call registerusers(@new_id,'Jason','Jones','5566','jones@gmail.com','sdfd','1');
select @new_id; 

用于插入第一个名称,它保持显示错误消息而不想插入数据。

我已经尝试if(firstname is null),但它没有奏效,因为它通过了消息传递。例如,如果我使用

set @new_id = null;
call registerusers(@new_id,'','Jones','5566','jones@gmail.com','sdfd','1');
select @new_id;  

对于它经历的名字为空,它假设显示错误消息。如果我用过

set @new_id = null;
call registerusers(@new_id,'Jason','Jones','5566','jones@gmail.com','sdfd','1');
select @new_id;  

它设法通过。

0 个答案:

没有答案