5

Just learning about triggers and I'm created the following trigger;

CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where msisdn = new.msisdn order by dat DESC limit 1);
END;

However the value does not appear to be getting updated. Any ideas?

2 Answers 2

10

I've actually managed to solve this myself. Here is the updated code

CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON `incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where crm_record.msisdn = new.msisdn order by dat DESC limit 1);
END;

I needed to specify the table name prior to the column value on line 5.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you have a typo.

You have entered incremental with a trailing backtick instead of enclosing it in backticks.

It is likely that your trigger is now bound to a table called incremental` which I am assuming does not exist.

Since you have ruled out the above. I see that the UPDATE keyword is missing. Add UPDATE table before your SET line. Replace table with the name of your table.

2 Comments

That's actually a typo in this entry, it was bound to the correct table
Tried that and got the following error Can't update table incremental in stored function /trigger because it is already used by statement which invoked this function/trigger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.