MySQL stored procedure returns multiple records while straight query returns one

时间:2017-06-07 11:06:22

标签: mysql stored-procedures uuid

If I create this stored procedure below and the run it I get back both rows from the table when it should be just one query.

    CREATE PROCEDURE `getUserName`(IN Guid VARCHAR(36), IN GameID INT(11))  
    BEGIN  
        SELECT `Name` as UserName, `GUID` as guid  
        FROM playerdata  
        WHERE `GUID` = Guid AND `GameID` = GameID;  
    END

    CALL getUserName('86fd1007-4a9c-11e7-b2e2-1803733c2d41', 1001);

Returns See Image

However if I just run it as a regular query

    SELECT `Name` as UserName, `GUID` as guid
    FROM playerdata
    WHERE `GUID` = "86fd1007-4a9c-11e7-b2e2-1803733c2d41" AND `GameID` = 1001;

Returns See Image

Table Data and Query Results

I am baffled as to the behavior considering... at least to me they are the same thing.

1 个答案:

答案 0 :(得分:1)

CREATE PROCEDURE `getUserName`(IN IN_Guid VARCHAR(36), IN IN_GameID INT(11))  
    BEGIN  
        SELECT `Name` as UserName, `GUID` as guid  
        FROM playerdata  
        WHERE `GUID` = IN_Guid  AND `GameID` = IN_GameID ;  
    END

    CALL getUserName('86fd1007-4a9c-11e7-b2e2-1803733c2d41', 1001);

尝试以上代码。

如果输入参数与列名具有相同的名称,它最终将评估等效于1=1,因此没有条件含义。均GameID = GameID1=1都是等价的。 因此,请始终尝试使输入和输出参数名称与特定表的列名不同。

我试过它工作正常。

希望这会对你有所帮助。