views:

1943

answers:

1

I've created a trigger that resembles the following:

delimiter //
CREATE TRIGGER update_total_seconds_on_phone AFTER INSERT
ON cdr 
FOR EACH ROW 
BEGIN
IF NEW.billsec > 0 AND NEW.userfield <> NULL THEN
UPDATE foo
SET total_seconds = total_seconds + NEW.billsec
WHERE phone_id = NEW.userfield;
END IF;

END;//

It seems to go through okay. However it doesn't appear to be invoking when I need it to. Here's an example:

mysql> select total_seconds from foo where phone_id = 1;
+---------------+
| total_seconds |
+---------------+
|             0 | 
+---------------+
1 row in set (0.00 sec)

mysql> insert into cdr (billsec, userfield) VALUES(60, 1); Query OK, 1 row affected, 12 warnings (0.00 sec)

mysql> select total_seconds from foo where phone_id = 1;
+---------------+
| total_seconds |
+---------------+
|             0 | 
+---------------+

EDIT: The warnings in this particular case do not affect the trigger. It's mostly additional columns which do not have default values in the cdr table that cause the warnings. For the sake of simplicity, I kept my INSERT statement brief.

+1  A: 

There's 12 warnings generated. What are they?

ETA: Oh, wait... you need to use is not null rather than <> null. Any comparison with null will always return null.

KernelM
Odd, that did it. Any idea why IS NOT NULL works in this case but <> NULL does not?Thanks!
As KernelM said, any comparison to NULL returns NULL. That means both = NULL and <> NULL are always NULL, which is effectively not true. This is standard SQL behavior.
Bill Karwin