title: MySQL Stored Procedure - Delete Ignoring 'WHERE' Clause
body: <p>We have a stored procedure "GetToken", that is called when users log in to our site.  This stored procedure generates a GUID (returning it to the client) and inserts an entry in to our "TokenIndex" table.  The GUID is only valid for an extended period of time, so each time the user logs in we generate a new GUID and delete all prior "TokenIndex" entries for that particular user.  The issue we're coming across (and it's driving us crazy) is that the the delete statement is deleting <em>all</em> entries inside the "TokenIndex" table, as if it's ignoring the <code>WHERE LoginID = loginId</code> clause inside the <code>DELETE FROM TokenIndex</code> statement.  See below for our stored procedure:</p>
  
  <pre><code>CREATE DEFINER=`arcanatekauth`@`%` PROCEDURE `GetToken`(    
     IN _username NVARCHAR(100),   
     IN _password TINYBLOB   
  )       
  BEGIN
     declare guid CHAR(36);    
     declare clientId int;
     declare loginId int;
     declare clientConnectionString nvarchar(500);
  
     Select li.ClientID, li.ID INTO clientId, loginId
     FROM LoginIndex li
     WHERE li.UserName = _username and li.Password = _password;
  
     if(clientId &gt; 0)
     then
     begin
        SET guid = UUID();            
        DELETE FROM TokenIndex
        WHERE LoginID = loginId;
  
        SELECT ci.ConnectionString INTO clientConnectionString
        FROM ClientIndex ci
        WHERE ci.ID = clientId;
  
        INSERT INTO TokenIndex (Token, LoginID, ConnectionString, ExpirationDtTm)
        VALUES (guid, loginId, clientConnectionString, NOW() + INTERVAL 1 HOUR);
     end;
     end if;
  
     select guid;
  END
  </code></pre>