How to declare a value in a trigger in mysql? - 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 ;

Related

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.

MySQL Trigger creation issue

Getting this error when trying to create the trigger- #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 'DECLARE orderCount INT;
SET orderCount = (SELECT COUNT(Order_No) FROM `order' at line 5
delimiter /
CREATE TRIGGER CountOrders
BEFORE INSERT ON order
DECLARE orderCount INT;
SET orderCount = (SELECT COUNT(Order_No) FROM `order` WHERE Outlet = 1 AND
complete ='FALSE');
BEGIN
IF orderCount > 3
THEN SIGNAL 'To many orders pending'
END;
/
delimiter ;
Any suggestions would be much appreciated. Thanks.
You have couple of issues in the trigger. Here is one with modification.
The declare part should be after begin , and order is a reserved word so need to wrap in backticks
delimiter //
CREATE TRIGGER CountOrders BEFORE INSERT ON `order`
for each row
begin
DECLARE orderCount INT;
SELECT COUNT(Order_No) into orderCount FROM `order`
WHERE Outlet = 1 AND complete ='FALSE' ;
IF orderCount > 3 then
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'To many orders pending';
end if;
end ;//
delimiter ;
https://dev.mysql.com/doc/refman/5.5/en/signal.html

Syntax error when creating a MySQL trigger

I have some problems trying to do a trigger.
Here is my MySQL query for the trigger:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN (
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE (temp%6<>0) temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
)
END IF;
END //
And the error is
#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 '; WHILE (temp%6<>0) temp=temp+1; END WHILE; UPDATE strutture SET ' at line 8
Does someone have an idea why I'm getting this error?
Remove the parentheses around your THEN clause. Also your WHILE clause is syntactically incorrect:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE temp%6<>0 DO
SET temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
END IF;
END //

Trigger in mysql complain

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

Error in Sql Query - Can't find the solution

I'm having big troubles to find the error in my sql query.
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SELECT count(*) FROM passenger WHERE Booking IN(SELECT Id FROM booking WHERE
Flight = (SELECT Flight FROM booking WHERE Id = bookingID)) INTO numberBooked;
IF (numberBooked > 59) THEN SET status =”No free seats”;
ELSE SET status =”OK”;
END IF;
RETURN status;
END;
I get this error message:
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 'free seats”; ELSE SET status =”OK”; END IF;
RETURN status; END' at line 10
I would appreciate some help.
Thanks in advance
don't for get to change the DELIMITER
use single quotes
example,
DELIMITER $$
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SET numberBooked =
(
SELECT count(*)
FROM passenger
WHERE Booking IN
( SELECT Id
FROM booking
WHERE Flight = (SELECT Flight FROM booking WHERE Id = bookingID)
)
);
IF (numberBooked > 59) THEN
SET status = 'No free seats';
ELSE
SET status = 'OK';
END IF;
RETURN status;
END $$
DELIMITER ;