MySQL Trigger creation issue - mysql

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

Related

MySQL Trigger : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I have problems creating triggers in MySQL 5.1.73
This is de syntax:
DELIMITER $$
CREATE TRIGGER `discount2`
BEFORE
INSERT ON `order_item`
FOR EACH ROW
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET #alumno := (SELECT user_id from `order` where `order`.id = NEW.order_id)
SET #profesor := (SELECT id_profesor from user where user.id = #alumno)
SET #dto := (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = #profesor)
SET.NEW.descuento = SELECT CAST((((#dto)*(NEW.pvp))/100) AS DECIMAL(10,2))
$$
DELIMITER ;
But there is some 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 'DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET #alu' at line 5
Someone can help me, please?
Thanks u
Note this syntaxs but may not 'work'
drop trigger if exists discount2;
DELIMITER $$
CREATE TRIGGER `discount2`
BEFORE
INSERT ON `order_stage`
FOR EACH ROW
begin
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET alumno = (SELECT user_id from `order` where `order`.id = NEW.order_id);
SET profesor = (SELECT id_profesor from user where user.id = alumno);
SET dto = (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = profesor);
SET NEW.descuento = (SELECT CAST((((dto)*(NEW.pvp))/100) AS DECIMAL(10,2)));
end $$
DELIMITER ;
The syntax to create a BEFORE INSERT Trigger in MySQL is:
CREATE TRIGGER trigger_name
BEFORE INSERT
ON table_name FOR EACH ROW
BEGIN
-- variable declarations
-- trigger code
END;
If you want to execute multiple statements in your trigger, you should use BEGIN and END.
The Reason for using DELIMITER $$ before and after the trigger declaration is to change the delimiter from ; (default) to $$.
If you didn't change the DELIMITER, the first semicolon in your trigger code may be interpreted as the end of trigger declaration. Thus the code following may result in syntax errors.
DELIMITER $$
CREATE TRIGGER `discount2`
BEFORE
INSERT ON `order_stage`
FOR EACH ROW
BEGIN
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET alumno = (SELECT user_id from `order` where `order`.id = NEW.order_id);
SET profesor = (SELECT id_profesor from user where user.id = alumno);
SET dto = (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = profesor);
SET NEW.descuento = (SELECT CAST((((dto)*(NEW.pvp))/100) AS DECIMAL(10,2)));
END;
$$
DELIMITER ;
Read More Here

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.

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 ;

stored procedure error message

I have implemented the stored procedure as below, however i am getting the following error message when i try applying it:
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 'WHERE symbol_id = id GROUP BY symbol_id;
I did some debugging and found that it was caused by the #max variable which i am trying to write the result into, however i do not see anything wrong with the syntax, can anyone please advise?
DROP PROCEDURE IF EXISTS `GENERATE_REPORT`;
DELIMITER $$
CREATE DEFINER=CURRENT_USER PROCEDURE `GENERATE_REPORT`()
BEGIN
DECLARE id INT;
DECLARE max INT;
DECLARE at_end BIT DEFAULT 0;
DECLARE cur CURSOR
FOR SELECT symbol_id from trade;
DECLARE CONTINUE HANDLER
FOR SQLSTATE '02000' SET at_end=1;
OPEN cur;
FETCH cur INTO id;
WHILE (NOT at_end) DO
SELECT SUM(quantity) FROM trade INTO **#max** WHERE symbol_id = id GROUP BY symbol_id;
FETCH cur into id;
END WHILE;
CLOSE cur;
END
$$
DELIMITER ;
You have incorrect syntax in your SELECT ... INTO:
Change
SELECT SUM(quantity)
FROM trade
INTO #max -- Incorrect placement
WHERE symbol_id = id
GROUP BY symbol_id;
To
SELECT SUM(quantity)
INTO #max -- Correct placement
FROM trade
WHERE symbol_id = id
GROUP BY symbol_id;
The INTO should come right after the SELECT and before the FROM

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 ;