views:

10

answers:

1

Hello everyone,

Is there a way to trace MySql triggers? I am stuck with the following one.

I want to calculate a column before insert concatenating two other columns, but there is problem calculated value is always null, i also need to check if one column has comma separated values, in that case i only want the first one, here is the code


BEGIN 
    declare pos int;
    pos = locate(',',new.Admin1Code, 1);
    if (pos = -1) then
        set new.StateCode = Concat(New.CountryCode,'.',New.Admin1Code);
    else
        set new.StateCode = Concat(New.CountryCode,'.',substring(New.Admin1Code,1,pos));
    end if;   
END;

What is the problem there?

Thanks.

A: 

LOCATE will never return -1 - if you try:

SELECT LOCATE(',', 'abbc')

...it returns 0. Use:

if (pos > 0) then
  set new.StateCode = Concat(New.CountryCode,'.',substring(New.Admin1Code, 1, pos));        
else
  set new.StateCode = Concat(New.CountryCode,'.',New.Admin1Code);
end if;

The documentation says as much:

Returns 0 if substr is not in str.

OMG Ponies
@Omg Ponies: thanks for your answer, i had pos = 0 but i changed by -1 because it seems not working, i realize now that mysql is caching somehow the values, i was making bulk loading from file to test trigger and it always shows the same result, that's what i thought it was not working, did you have the same issue with mysql before?
dev-cu
@dev-cu: I think the issue is the data you are testing - MySQL won't cache the values. Or it could be the bulk loading is bypassing the trigger?
OMG Ponies