I'm having trouble with a trigger in MySQL Workbench. It's not running and there's a red X next to VALUES in my code. Can someone help me figure out what's wrong with my syntax?
Here's my trigger:
DELIMITER $$
CREATE TRIGGER ApprovedTopicPublisher
AFTER INSERT ON AwaitingTopics
FOR EACH ROW
BEGIN
INSERT INTO ActiveTopics (Header,Summary,Inquiry1,Inquiry2,Inquiry3,Inquiry4,CreatorID)
VALUES (NEW.Header,NEW.Summary,NEW.Inquiry1,NEW.Inquiry2,NEW.Inquiry3,NEW.Inquiry4,NEW.CreatorID)
FROM AwaitingTopics
WHERE NEW.TopicStatus = 'APPROVED'
END;$$
DELIMITER ;
I've tried updating it but I'm still stuck. Any ideas what could be causing this?
I have encountered similar issues with triggers in MySQL Workbench before, and it seems that the problem lies in your syntax. Instead of using a FROM clause with your INSERT statement, you need to remove it because it is not valid in an INSERT … VALUES statement. Also, ensure that you have a semicolon after the VALUES clause and another after the END statement. Including an IF check for the TopicStatus before performing the INSERT can help clarify the condition. For example:
DELIMITER $$
CREATE TRIGGER ApprovedTopicPublisher
AFTER INSERT ON AwaitingTopics
FOR EACH ROW
BEGIN
IF NEW.TopicStatus = 'APPROVED' THEN
INSERT INTO ActiveTopics (Header,Summary,Inquiry1,Inquiry2,Inquiry3,Inquiry4,CreatorID)
VALUES (NEW.Header,NEW.Summary,NEW.Inquiry1,NEW.Inquiry2,NEW.Inquiry3,NEW.Inquiry4,NEW.CreatorID);
END IF;
END$$
DELIMITER ;
This revision should help resolve the errors you are experiencing.
I’ve run into this issue before. The problem is likely with your trigger syntax. You’re mixing INSERT…VALUES with a SELECT-like structure, which isn’t correct. Here’s a cleaner version that should work:
CREATE TRIGGER ApprovedTopicPublisher
AFTER INSERT ON AwaitingTopics
FOR EACH ROW
BEGIN
IF NEW.TopicStatus = ‘APPROVED’ THEN
INSERT INTO ActiveTopics (Header, Summary, Inquiry1, Inquiry2, Inquiry3, Inquiry4, CreatorID)
VALUES (NEW.Header, NEW.Summary, NEW.Inquiry1, NEW.Inquiry2, NEW.Inquiry3, NEW.Inquiry4, NEW.CreatorID);
END IF;
END$$
This removes the unnecessary FROM clause and adds an IF statement to check the TopicStatus. Also, make sure you’re using the correct DELIMITER statements before and after your trigger definition. Let me know if you still have issues after trying this.
hey man, looks like ur missing a semicolon after the END statement. try adding that and see if it helps. also, u dont need the FROM clause in an INSERT…VALUES statement. remove that line and it should work. good luck!