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 ;
Related
I have a problem with a stored procedure, both in syntax and logic. I got the error below when trying it on sqlfiddle.com.
Also I'm not sure if the logic is right. Is there anyway I can check by entering the ID and the query will return a table which row that contains the ID has been deleted?
create a procedure named delete_id which deletes the row that goes by the ID
CREATE PROCEDURE DELETE_ID (ID_INPUT IN INT)
AS
BEGIN
DELETE FROM TABLE ALBUM WHERE ID=ID_INPUT;
END;
Expected result: Enter an Id and SQL will delete the row which contains the ID
Actual Result:
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 'TABLE ALBUM WHERE ID=ID_INPUT' at line 3
You need to add "DELIMITER //" at the start on the code and "//" at the end. So your code should look like
DELIMITER //
CREATE PROCEDURE `DELETE_ID`(IN `ID_INPUT` INT)
BEGIN
DELETE FROM `ALBUM` WHERE `ID` = ID_INPUT;
END //
Trying to create a simple trigger that after a table update, it checks if a count (InvalidLogins) is over 4, and if so, sets the field 'LockedOut' to 1. Below is how I am attempting to create the trigger, this is my first trigger for MySQL though and I can't get it to work.
CREATE TRIGGER update_user_lockout after update ON members
FOR EACH ROW SET LockedOut = 1 where invalidLogins < 4
The error that MySQL returns is:
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 '.LockedOut = 1 where invalidLogins < 4' at line 2
delimiter |
CREATE TRIGGER update_user_lockout before update ON members
FOR EACH ROW
IF NEW.invalidLogins < 4 THEN
SET NEW.LockedOut = 1;
END IF;
END
|
delimiter ;
The current record can be referenced with NEW
Choose another delimiter than ;. Otherwise the trigger definition will end at the first ; which is too early
Use a before trigger to change values before updating
m writing following lines for creating a trigger:
create trigger notify after insert on applications
for each row
begin
insert into notifications SET sno=1;
end;
but everytime i get following error:
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 4
line 4 is - insert into notifications SET sno=1
MySQL is confused about the delimiters. It thinks the first ; is the end of the TRIGGER declaration (which it is not). Use delimiter to change it temporarily, and later to change it back:
delimiter |
create trigger notify after insert on applications
for each row
begin
insert into notifications (sno) values (1);
end;
|
delimiter ;
More in the documentation.
I try to create trigger in MySql but get the following 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 'DELIMITER' at line 1
DELIMITER $$
CREATE TRIGGER library_update
AFTER UPDATE ON wq6vt_vehiclemanager_vehicles
FOR EACH ROW
BEGIN
INSERT IGNORE INTO wq6vt_vehiclemanager_library (maker, model) VALUES(NEW.maker, NEW.vmodel);
INSERT INTO wq6vt_vehiclemanager_library_data (co2_class)
SELECT co2_class FROM wq6vt_vehiclemanager_vehicles
WHERE maker = NEW.maker AND vmodel = NEW.vmodel;
END $$
DELIMITER;
The first query in the trigger do not lead to errors, but second one does. There is some problem with SELECT inside of INSERT ...I think so
there should be a space between the keyword and the symbol,
DELIMITER ;
-- ^ space in between here
I am trying to set up a trigger on a table to copy the contents of a row into another table.
I have the following:
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END;
This returns the following error though:
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 3
I can't work out where I'm going wrong with this. Any ideas?
Try changing the delimiter
DELIMITER $$
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW
BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END $$
DELIMITER ;
and as far as your privileges go, run this query
SHOW GRANTS;
If SUPER is not there, you could
request your DBA to add that privilege for you
have your DBA create the trigger for you