I am creating a trigger for my tables:-
bank
Bank(pin, deposit, withdraw, balance, accno*, sno)
and
balance
Balance(accno*,balance)
I want to update the value of balance in my balance table after insertion in the bank table.
I am using a MySQL server (wamp64 mysql8.0.18)
mysql> create trigger update_account
-> after insert on bank
-> begin
-> update balance as a
-> set a.balance=(case
-> when new.withdraw=1 then a.balance-new.withdraw
-> else a.balance+new.withdraw
-> end)
-> where a.accno = new.accno;
but the above code gives me the following error:-
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 'update balance a
set a.balance = (case
when new.withdraw=1 then a.balance - new.' at line 3
You should create trigger query same my code:
create trigger update_account
after insert
on bank
for each row
begin
update balance as a
set a.balance=(IF(new.withdraw = 1, a.balance - new.withdraw, a.balance + new.withdraw))
where a.accno = new.accno;
END;
And if you have only two cases, you should IF, not need Case when
DELIMITER $$
DROP PROCEDURE my_procedure$$
create trigger update_account
after insert
on bank
for each row
begin
update balance as a
set a.balance= a.balance + new.withdraw + new.deposit
where a.accno=new.accno;
END$$
DELIMITER ;
My code is now working
thank you all
Your trigger contains ONE statement. So it does NOT need in BEGIN-END block and DELIMITER re-assigning:
CREATE TRIGGER update_account
AFTER INSERT
ON bank
FOR EACH ROW
UPDATE balance
SET balance = balance + new.withdraw + new.deposit
where accno=new.accno;
Related
enter code here
DELIMITER $$
DROP TRIGGER IF EXISTS `Update_Status`$$
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`))
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET(`L_Seen`=new.`Cap_time`) WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`);
END$$
I have Created this After insert trigger On Occurrence _time I want to store first time of occurrence of a day and last time of occurrence of Day in Total_time table but getting this Error
"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 '(L_Seen=new.Cap_time) WHERE (SSN=new.SSN and Day_Date=new.`Day_Date...' at line 7" ***
Instead of SET(L_Seen=new.Cap_time) Please use SET L_Seen=new.Cap_time. You missed end if also. Below code is working.
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`)
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET `L_Seen`=new.`Cap_time` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`;
end if;
end
DB-Fiddle:
create table occurance_time(SSN varchar(50),Name varchar(50),Day_Date date,Cap_time time);
CREATE TRIGGER Update_Status AFTER INSERT ON occurance_time
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT F_Seen FROM total_time WHERE SSN=new.SSN and Day_Date=new.Day_Date)
THEN
INSERT INTO total_time (SSN,Name,Day_Date,F_Seen) VALUES(new.SSN,new.Name,new.Day_Date,new.Cap_time);
ELSE
UPDATE total_time SET L_Seen=new.Cap_time WHERE SSN=new.SSN and Day_Date=new.Day_Date;
end if;
end
db<>fiddle here
I'm trying to create a BEFORE INSERT trigger and getting errors that I don't understand.
The MySQL to create the trigger looks like this:
CREATE TRIGGER `GetPolyLinkID` BEFORE INSERT ON `TableA`
FOR EACH ROW
BEGIN
INSERT INTO TableB () VALUES ();
NEW.PolyLinkID = last_insert_id();
END
The error I'm getting is
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 1
You forgot SET ... begore NEW.PolyLinkID = last_insert_id();
And
For creating complex trigger , you must use the delimiter keyword .
This is a example from rom https://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html .
delimiter //
CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
IF NEW.amount < 0 THEN
SET NEW.amount = 0;
ELSEIF NEW.amount > 100 THEN
SET NEW.amount = 100;
END IF;
END;
//
delimiter ;
Screen shot with sequel pro
I have two table stock detail and purchase.
When I'm trying to create a trigger on the table purchase it will seems an error that
"#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 "
Hear is my code
create TRIGGER afterinsert after insert on purchase for each row
BEGIN
set #qnty=(select quantity from stock_detail where brand_name = new.brand);
if(#qnty>=1) then
update stock_detail set quantity=#qnty+new.quanty;
end if;
END;
Try this:
delimiter //
create TRIGGER afterinsert after insert on purchase for each row
BEGIN
set #qnty=(select quantity from stock_detail where brand_name = new.brand);
if(#qnty>=1) then
update stock_detail set quantity=#qnty+new.quanty;
end if;
END;//
delimiter ;
For more info refer: http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
Any ideas what's wrong with this query? I searched around stackoverflow and it seems everyone is using this and it works.
mysql> CREATE TRIGGER upd_entry
-> AFTER UPDATE ON entry FOR EACH ROW
-> BEGIN
-> UPDATE entry
-> SET count = count +1
-> WHERE id = NEW.id
-> END;
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 7
Run this first to change the delimiter
DELIMITER //
create the trigger
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry
SET count = count +1
WHERE id = NEW.id;
END//
After change it back
DELIMITER ;
forget ; at end of line
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry SET count = count +1 WHERE id = NEW.id;//forget to close line ;
END;//
delimiter;
Mysql Trigger
This is refrence to my earlier question
rails callbacks not getting executed
Now since the framework is not working some time I rather thinking of writing a trigger to do this
so my trigger look like this
DELIMITER $$
CREATE TRIGGER sales_earning AFTER INSERT ON sales_transactions FOR EACH ROW
BEGIN
DECLARE earning INT;
SET earning = (select sales_earning from payouts where id = NEW.payout_id);
earning = earning + NEW.amount
UPDATE payouts SET sales_earning = earning where id = NEW.payout_id ;
END $$
DELIMITER ;
Anyone has a clue as what the issue is with the trigger It complaining
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 '= earning + NEW.amount ;
FYI there is parent - child relationship between payouts and sales_transactions
Any helps
CREATE TRIGGER sales_earning AFTER INSERT ON sales_transactions FOR EACH ROW
UPDATE payouts
SET sales_earning = sales_earning + NEW.amount
WHERE id = NEW.payout_id
;
You are missing a ; after earning = earning + NEW.amount
And you should do:
SET earning = (select sales_earning from payouts where id = NEW.payout_id) + NEW.amount;
UPDATE:
My answer points to what is wrong with your trigger, but if that's all you want to do with earnings, you should simply do it like eggyal's answer is pointing out