vendredi 30 mars 2018

MySQL: SET Variable in if exists condition

I am trying to set the variables while checking the if exists condition. I have used := to set the variables, however, it for some reason it seems that when I try to set the variable in the if exists condition, it displays the result for the previous query. Following is the code snippet from the stored procedure.

if exists (select @AccountVerified := AccountVerified, @IsActive := IsActive  from tblUserLookUp where UserName = inUserName) then
        begin
            select "1: From if condition", @AccountVerified, @IsActive;

            select @AccountVerified := AccountVerified, @IsActive := IsActive  from tblUserLookUp where UserName = inUserName;
            select "2: From select condition", @AccountVerified, @IsActive;
            select @AccountVerified, @IsActive;

            if @AccountVerified = 0 then
                set outErrorCode = 3;
            elseif  @IsActive = 0 then
                set outErrorCode = 4;
            end if;
        end;
    else
        set outErrorCode = 1;
    end if;

I observed this by trying to print the values through the select statement after the if condition and after again running the select query on the table. The

2: From select condition

seems to display the actual current results However,

1: From if condition

seems to display the value from previous query.

Is there any concept of variable caching or it is that you cannot set variables in the if condition? I am also open to any other recommendation that you might have.

The only reason to do this is to save that select query on the same table as that of the if exists select query.

Aucun commentaire:

Enregistrer un commentaire