Trigger in mysql complain - mysql

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

Related

mysql syntax ERROR 1064 while creating a trigger

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;

MySQL CREATE TRIGGER IF variable is greater than set value error 1064

I have been searching this site and Google for over an hour trying to figure out why I get this message:
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 '#msg = 'HIGH VALUE CUSTOMER - SALES GREATER THAN
$10K' IF (SALES > 10000) THEN' at line 5
...when running the SQL to create this trigger:
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100)
#msg = 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K'
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = #msg
ENDIF
This is for an assignment that is due by midnight tomorrow night and I'm stumped because everything I have tried is in the same syntax as the examples and I'm not trying to create a complicated trigger. Please help!?!?!
This query has so many errors, syntax, variable define, delimiter etc... Even the inner logic is not clear as well, you should read about CREATE PROCEDURE and CREATE FUNCTION Syntax first, here I only fix some syntax errors:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100) DEFAULT 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = msg;
END IF;
END;
||
DELIMITER ;
Edit:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
BEFORE INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100) DEFAULT 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
IF (NEW.SALES > 10000) THEN
SET NEW.CUSTOMERNOTES = msg;
END IF;
END;
||
DELIMITER ;
I ended up figuring it out...here's what worked for me:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE CUSTOMERNOTES VARCHAR(100);
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
END IF;
END;
||
DELIMITER ;
Thanks everyone that responded for pointing me in the right direction.

Error in mysql while creating a trigger

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

How to declare a value in a trigger in mysql?

I am trying to write a trigger for my database using mysql and phpmyadmin. I have a database with a table for a client's information in which I store a credit card number and a table for hotel reservations where I store the client's way of paying. I want my trigger to activate after update on client's info and if the credit card number is deleted to change the way of payment. The problem is that phpmyadmin does not compile it.
CREATE TRIGGER after_credit_card_update
AFTER UPDATE
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or (new.Credit_Card_Number is null)) then
select into cl_id
select reservation.Client_Id from
reservation where ((reservation.Client_Id = new.Client_Id) and (reservation.Payment = 'Credit'))
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$
I believe that the problem is when I am declaring my value: cl_id
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 'into cl_id select reservation.Client_Id from reservation where ' at line 8
FIXED :
DELIMITER $$
CREATE TRIGGER after_credit_card_update
AFTER UPDATE
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or
new.Credit_Card_Number is null)) then
select reservation.Client_Id into cl_id from reservation
here ((reservation.Client_Id = new.Client_Id) and
reservation.Payment = 'credit'))
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$
DELIMITER ;
Not going into detail here, but you need to add delimiter statements before and after your trigger - so MySQL will know how to parse the code. For example:
DELIMITER $$
CREATE TRIGGER after_credit_card_update
AFTER UPDATE
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or
new.Credit_Card_Number is null)) then
select into cl_id
select reservation.Client_Id from
reservation where reservation.Client_Id = new.Client_Id
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$
DELIMITER ;

Creating a trigger in mysql

I'm trying to create a trigger on a table after inserting, which gonna update another table.
this is the code I tried :
delimiter |
CREATE TRIGGER augmenter_quantite_article AFTER INSERT
ON LigneInterventaire
FOR EACH ROW BEGIN
DECLARE #qte AS INTEGER;
DECLARE #code AS INTEGER;
SELECT #qte = qteInv FROM INSERTED;
SELECT #code = codeArt FROM INSERTED;
UPDATE Article SET qteArt = qteArt + #qte WHERE codeArt = #code;
END;
|
delimiter ;
but I get this 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 '#qte AS INTEGER; DECLARE #code AS INTEGER; SELECT #qte = qteInv
FROM INSERTED; S' at line 4
Try
CREATE TRIGGER augmenter_quantite_article
AFTER INSERT ON LigneInterventaire
FOR EACH ROW
UPDATE Article
SET qteArt = qteArt + NEW.qteInv
WHERE codeArt = NEW.codeArt;
Here is SQLFiddle demo.
MySql has somewhat limited trigger implementation. There are no virtual tables inserted and deleted per se as in SQL Server. Instead you can access old and new values of a row that is being inserted or updated with keywords OLD and NEW respectively.
Since it boiled down to only one UPDATE statement you no longer need to use BEGIN END block. Another plus - you don't need change DELIMITER either ;)