2016-09-12 37 views
0

我具有上checkfirstnamecount邏輯問題的問題= 1,例如:!與邏輯有問題在MySQL存儲過程

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','[email protected]','1243','U'); 

我不斷看到,上面寫着相同的顯示信息:

填寫名字

它假設執行插入數據。

回答

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 
+0

我測試了第一個名字計數的傑裏這樣的:從我們SELECT COUNT(名字)到checkfirstnamecount ers 其中FirstName ='jerry';計數的結果是2,我試過如果(checkfirstnamecount = 0),這次我嘗試呼叫registerusers(@ new_id,'Jerry','Johnson','5566','[email protected]','1243 」, 'U');結果是我不斷得到相同的消息說「填寫名字」 –

+0

@DavidJones,這是正確的,因爲已經有該名稱的記錄目前它不應該執行插入 – Rahul

+0

它假設插入數據和什麼如果該人具有相同的名字,但姓氏不同。它就像這樣If(checkfirstnamecount = 0)then SIGNAL SQLSTATE'45000' SET MESSAGE_TEXT ='填寫名字'; –