MySQL AFTER INSERT Trigger - select INTO using NEW values not working - mysql

In MySQL:
I have an interview table, and
when a new interview is created (a row inserted into interview),
I want to add rows to a child table interview_questions table linking to the question definitions in interview_questions_template.
I'm trying to do this in an AFTER INSERT trigger on interview, and mysql is saying I have a syntax error at the end of my INSERT INTO ... SELECT statement.
I've tried joining with NEW, thinking it might be a table, but that didn't work either. Gander at the code?
CREATE TRIGGER interview_AFTER_INSERT AFTER INSERT ON interview
FOR EACH ROW
BEGIN
INSERT INTO interview_questions (id_interview, id_candidate, id_question_def, s_userid)
SELECT NEW.id_interview, NEW.id_candidate, interview_question_template.id_question_def, NEW.s_userid
FROM interview_question_template;
END
The error mysqlworkbench is showing is "missing 'semicolon'", underlining interview_question_template after the FROM.
The execution error says there is an error on that line after ' '.

You are doing fine. Just wrap the whole thing in a delimiter block. The below survives the 1064 Error.
DELIMITER $$
CREATE TRIGGER interview_AFTER_INSERT AFTER INSERT ON interview
FOR EACH ROW
BEGIN
INSERT INTO interview_questions (id_interview, id_candidate, id_question_def, s_userid)
SELECT NEW.id_interview, NEW.id_candidate, interview_question_template.id_question_def, NEW.s_userid
FROM interview_question_template;
END
$$
DELIMITER ;
As for the importance of Delimiters, see the bottom of This Post of mine. I wrote it more eloquently elsewhere, just can't find a reference to it. And the mysql manual has little about it with any verbosity for the average human.
I am sorry for posting the obvious. Sometimes people just have to see it :P

The other thing I did was to simply remove the BEGIN/END and it also worked like a charm.

Related

MySQL 5.7 Create Trigger Syntax Error?

I've been trying to create a simple BEFORE INSERT trigger on a database table (MySQL v 5.7 ) but I keep receiving a vague "#1064 ... syntax error" message which doesn't help resolve the issue.
Here's the SQL:
CREATE OR REPLACE TRIGGER `CREATE_QUIZ_TRIG` BEFORE INSERT ON `quiz`
FOR EACH ROW BEGIN
SET NEW.ACTIVE = UPPER(NEW.ACTIVE);
SET NEW.CREATED = NOW();
END
/
All I'm trying to do is enforce a column to uppercase and then insert the current date & time into a timestamp column. I've been following the documentation from:
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
and realise that for multi-statement expression I have to redefine the delimiter at the beginning of the trigger's creation but the same '#1064' error occurs.
This is made even more confusing because when I use phpmyadmin's interface for creating the same trigger it works fine - but won't when I export the generated SQL and try to create the trigger using that!?
Thanks for any help
I didn't realise that, by default, phpmyadmin adds a ; delimiter which was breaking the ; used to end a statement within the BEGIN END block.

MYSQL trigger syntax - missing semicolon

I'm relatively new to MYSQL from an MS SQL background. Can someone please point out why I'm getting syntax errors here please? I'm sure I've done something very obviously wrong but I just can't see it:
I'm getting a missing semicolon syntax error on line 8:
CREATE TRIGGER trg_InsertProductWatchListPriceHistory
AFTER UPDATE
ON ProductWatchlist FOR EACH ROW
BEGIN
INSERT INTO ProductWatchListPriceHistory
(ProductWatchlistID,Price)
VALUES
(ProductWatchlistID,New.ProductPrice);
END;
Any help greatly appreciated.
You need to change your default delimiter to something else than ;. Otherwise your definition ends at the first ; which would make it incomplete.
delimiter |
CREATE TRIGGER trg_InsertProductWatchListPriceHistory
AFTER UPDATE
ON ProductWatchlist FOR EACH ROW
BEGIN
INSERT INTO ProductWatchListPriceHistory (ProductWatchlistID, Price)
VALUES (New.ProductWatchlistID, New.ProductPrice);
END
|
delimiter ;
Please check this bash script for generating the update trigger.
This script will require the database name and table names.
While using this script please update the username and password.
If using MySQL Workbench then Editor's create trigger under table design options should be used to avoid changing delimiters before and after.

Proper use of BEGIN and END in MySQL

I just switched from using Apache's Derby Database to MySQL and still getting familiar with the syntax. I read the documentation about triggers and I think I followed the syntax correctly. However, I'm having problems with BEGIN and END My insert trigger below doesn't work if I put BEGIN and ENDI even tried putting DELIMITER but it doesn't fix it.
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW
BEGIN
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
END;
Removing the BEGIN and END makes it work but I'm not able to take full advantage of the compound statements.
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
I'd appreciate any help.
Thanks.
Edited:
I tried to follow #Ilanatos advice which works but returns an error on first attempt. I had to refresh the phpmyadmin page to get rid of the error.
Below are the screenshots.
then if I refresh the page(both Firefox and Chrome), I see the trigger.
I don't think it should return an error message during execution of create trigger definition.
Try using the delimiter function when creating your trigger.
DELIMITER $$
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW BEGIN
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
END$$
DELIMITER ;

Unexpected END_OF_INPUT in MySQL trigger

I have searched for all the possible online solutions but I can't figure out the error in this trigger.
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END;
the first error appears at OLD.neat_link
syntax error, unexpected END_OF_INPUT, expecting ';'
and the second one at END;
syntax error, unexpected END
Any help would be appreciable, thanks.
That problem is due to interpreting individual statements. The CREATE TRIGGER statement is as such a single complete statement that must be sent as is to the server. Usually statement borders are recognized by the default delimiter (the semicolon). In case of stored programs however the semicolon is needed to separate inner statements. This would confuse the client as it cannot tell apart what is an inner statement of the stored program or a full statement as it must be sent as a whole to the server.
Hence the DELIMITER statement was introduced which only applies to clients (not the server, the server itself cannot parse this statement). It changes the default delimiter to one of your choice, leading so the client where to look for the statement's end. A typical case hence looks like this:
DELIMITER ;;
CREATE TRIGGER `ins_film` AFTER INSERT ON `film` FOR EACH ROW BEGIN
INSERT INTO film_text (film_id, title, description)
VALUES (new.film_id, new.title, new.description);
END;;
Their is only one statement in the body of the Trigger, so there is no need to use the BEGIN-END compound statement construct. Try this:
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`
another possible solution
DELIMITER $$
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END$$
DELIMITER ;

Trigger Syntax Error 1064, Using IF/THEN and Dates

Being a complete newbie to this, I thought I might be able to save my self a lot of headache by just asking for help...
When I try to run this I'm getting an Error 1064: You have an error in your SQL Syntax...
CREATE TRIGGER payroll_lock BEFORE DELETE OR INSERT OR
UPDATE ON timesheet_entry FOR EACH ROW IF entry_date < '2013-07-25'
THEN raise_application_error(-20001, 'Cannot modify old records.');
I would also consider other options for stopping the insert/update/delete if the record is before a given fixed date.
Thanks for any help in fixing this! And explanations are appreciated, I don't know much in this particular area.
you should CALL keyword before raise_application_error function.
another tip is you should use NEW or OLD keyword for update trigger.
I'm not sure but I don't think you can create multiple trigger at same command with OR(this is first time I see that).
DELIMITER //
CREATE TRIGGER payroll_lock BEFORE UPDATE ON wp_links
FOR EACH ROW
BEGIN
IF NEW.entry_date < '2013-07-25' THEN
CALL raise_application_error(-20001, 'Cannot modify old records.');
END IF;
END//