It's my first time that I try to use trigger on mysql (and generally I don't use mysql so much).
The version of mysql that I'm using is 5.5.40 and the code I'm using to create the trigger is:
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END;
Where field1 and field2 are two fields in both tables (tab1 and tab2) both archer(120).
When I try to execute this code I receive an error:
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '' at line 7
(line 7 is near "WHEN" keyword).
I checked several answer on stack overflow and other web sites, I tried with and without delimiters, and I still not able to create a trigger.
What's my syntax error?
Is there some way to have some more accurate mysql debugging? Actually mysql answer (like I just posted) is just "there is an error (1064)" but it doesn't let me know what is wrong.
you have to change the delimiter to use it in your sql statement :
DELIMITER //
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END//
DELIMITER ;
Related
I have the following MySQL query to create a trigger:
CREATE TRIGGER `updateParentLastInteractionOnInsert`
AFTER INSERT
ON `Post` FOR EACH ROW
BEGIN
UPDATE `Post` SET `lastInteraction` = CURRENT_TIMESTAMP WHERE `id` = new.`parentPostId`;
END;
When I execute the query, I get the following error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
Since this means literally nothing, I've done my own research:
When I remove everything in the trigger's body (the statements between "BEGIN" and "END"), the query is processed successfully and an empty trigger is created.
So I've tested the inner statement by running the following query with a similar syntax:
UPDATE `Post` SET `lastInteraction` = CURRENT_TIMESTAMP WHERE `Post`.`id` = 1
which was also successfully executed.
I don't know what's wrong here. If both the trigger and the inner statements are syntactically & logically correct, how can the top query be syntactically incorrect? Maybe update statements aren't compatible with triggers? I've looked into MySql's documentation and around the web and I couldn't find any relevant information.
What's wrong in my query?
#BillKarwin 's comment has the answer.
This works:
DELIMITER //
CREATE TRIGGER `updateParentLastInteractionOnInsert`
AFTER INSERT
ON `Post` FOR EACH ROW
BEGIN
UPDATE `Post` SET `lastInteraction` = CURRENT_TIMESTAMP WHERE `id` = new.`parentPostId`;
END //
I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following error:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;
I am trying to create in MySQL a 'custom' unique ID based on existing primary key (auto-incremented) 'id' field
delimiter //
CREATE TRIGGER update_id AFTER INSERT ON test
BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END;
//
delimiter ;
I am using PhpMyAdmin and I really don't know if it's a UI problem (I encountered some problems in the past while creating triggers in PhpMyAdmin) or I really do something wrong.
What I need is a custom PN field that is automatically updated when insert new records in table, based on some text prefix "PN-"
id PN other fields
-------------------------
...
...
...
1253 PN-1253
1254 PN-1254
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END' at line 2
I read in some other StackOverflow post that this will be impossible (to update) on the same table AFTER insert.
There is a solution with this? Thanks.
Get the new value and add it before insert
delimiter //
CREATE TRIGGER update_id BEFORE INSERT ON test
BEGIN
SET #pn:= ( SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='test'
AND TABLE_SCHEMA=DATABASE() );
SET new.PN= CONCAT('PN-',#pn);
END;
//
delimiter ;
Here
I just want to create a trigger for my phpmyadmin. Basically the intended function is whenever I update a credit column of my finance table, the balance column will be automatically updated for each row.
My program is:
CREATE TRIGGER UPDATE_BALANCE AFTER UPDATE financeofstudents
FOR EACH ROW
BEGIN
IF financeofstudents.credit > 0
AND
financeofstudents.credit <= financeofstudents.debit
THEN
UPDATE financeofstudents SET financeofstudents.balance = financeofstudents.debit- financeofstudents.credit;
END IF;
END
but it gives me this error:
Error
SQL query:
CREATE TRIGGER UPDATE_BALANCE AFTER UPDATE financeofstudents
FOR EACH ROW
BEGIN
IF financeofstudents.credit > 0
AND
financeofstudents.credit <= financeofstudents.debit
THEN
UPDATE financeofstudents SET financeofstudents.balance = financeofstudents.debit-financeofstudents.credit
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'financeofstudents
FOR EACH ROW
BEGIN
IF financeofstudents.credit > 0
AND
f' at line 1
I don't know where is the problem. Expert guys, please help me.
The error message is a useful hint here. It says .. for the right syntax to use near 'some text from your query starting with the first thing it didn't understand'.
In your case, you need the word ON before the name of the table.
CREATE TRIGGER update_balance
AFTER UPDATE
ON financeofstudents
FOR EACH ROW
If you observed that MySQL error messages are hard to interpret, you would be correct.
I have a table named sample_table and its columns are col_a, col_b, col_c.
I am trying to create a trigger in MySQL 5.6 which will update col_c(as concat(col_a,col_b)) on insertion of a row. Query which I have written for this is:
create trigger trg_sample_table
after insert on sample_table
for each row
begin
set col_c=concat(col_a,col_b)
end;
While running this sql, the error I am getting is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 6.
Could any one of you please help me to create a trigger as I need?
You need to make it a before insert. Try this:
create trigger trg_sample_table before insert on sample_table for each row
begin
set new.col_c=concat(new.col_a,new.col_b);
end;
You cannot modify the row that you just inserted in an after insert trigger. If you want to modify it, it needs to be done in a before insert.