syntax error on updating two tables with one trigger - mysql

hi i'm trying to update two different tables with one trigger but i keep getting syntax error and i'm not sure what i'm doing wrong
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 'UPDATE specialitys s
JOIN speciality_objects o
ON s.id = o.speciality' at line 2
triggers works fine if i just update one table but i cant put them both in one trigger
here is my trigger
DELIMITER $$
CREATE TRIGGER after_user_type_or_status_change
AFTER UPDATE
ON users
FOR EACH ROW
BEGIN
UPDATE stats s
SET
ee_counter =
CASE
WHEN NEW.type= 1 THEN ee_counter + 1
WHEN NEW.type= 3 && OLD.type = 2 THEN ee_counter + 1
WHEN OLD.type= 1 && NEW.type != 3 THEN ee_counter - 1
ELSE ee_counter
END ,
er_counter =
CASE
WHEN NEW.type= 2 THEN er_counter + 1
WHEN NEW.type= 3 && OLD.type = 1 THEN er_counter + 1
WHEN OLD.type= 2 && NEW.type != 3 THEN er_counter - 1
ELSE er_counter
END
WHERE
s.id = 1;
END $$
BEGIN
UPDATE specialitys sp
JOIN speciality_objects o
ON sp.id = o.speciality_id
JOIN users u
ON o.user_id = u.id
SET
counter =
CASE
WHEN NEW.status = 1 THEN counter + 1
ELSE counter
END
WHERE
sp.id = o.speciality_id ;
END $$
DELIMITER ;

The END $$ in the middle is ending the trigger definition. Remove it and the following BEGIN.

Related

Trying to resolve a syntax error for MYSQL update trigger

I am trying to create a trigger to update the Project table whenever the Assign table is updated, yet I keep receiving this error: 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 'END IF
Does anyone have any idea of why I am getting this syntax error? Please note that I am a beginner at this so my SQL statements may be entirely wrong. Also, I know for sure that my query being ran in my IF statement works because I've ran it on its own without being inside a trigger.
delimiter $$
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
#RUN THIS QUERY TO UPDATE MY TABLE
UPDATE Project p JOIN
(SELECT Project.projNo, COUNT(DISTINCT empid) as numEmployeeAssigned
FROM (Project LEFT JOIN Assign ON Project.projNo=Assign.projNo)
GROUP BY projNo
) as tb
ON p.projNo = tb.projNo
SET p.numEmployeeAssigned = tb.numEmployeeAssigned
#END QUERY
END IF
END$$
delimiter;
You can just increment or decrement the count:
UPDATE Project p
SET p.numEmployeeAssigned = p.numEmployeeAssigned +
(case when p.projNo = new.ProjNo then 1 else -1 end)
WHERE p.projNo IN (old.ProjNo, new.ProjNo) AND
old.ProjNo <> new.ProjNo;
Obviously, your code is missing semi-colons after the UPDATE statement and after the END IF.
However, let me suggest that the UPDATE statement could most likely be simplified. As I understand your code, you want to update the count of employees in Project whenever an employee changes its assignment. If so, there is no need to actually query assign. You can just use conditional logic like this:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo);
END IF;
END$$
You could even remove the IF statement and put that condition in the query directly:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo) AND OLD.projNo <> NEW.projNo
END$$

MySQL Create Trigger Syntax Error (Last Line)

I'm creating a MySQL trigger designed to update various tables with a new value if certain values are changed by an UPDATE query. I keep receiving the following syntax error for this particular trigger:
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 29
Line 29 in this case is the line of the END statement.
This is my full code:
DELIMITER $$
CREATE TRIGGER update_selling_prices BEFORE UPDATE ON t1
FOR EACH ROW
BEGIN
DECLARE update_price INT DEFAULT 0;
DECLARE selling_price_1 DECIMAL(10, 3) DEFAULT 0.000;
DECLARE selling_price_2 DECIMAL(10, 3) DEFAULT 0.000;
IF (OLD.rrp_price <> NEW.rrp_price OR OLD.discount_1 <> NEW.discount_1 OR OLD.discount_2 <> NEW.discount_2 OR OLD.net_price <> NEW.net_price OR OLD.markup <> NEW.markup OR OLD.delivery_cost <> NEW.delivery_cost) THEN
SET update_price = (SELECT b.is_auto_update FROM price_categories c INNER JOIN brands b ON b.brand_name = c.brand_name WHERE c.id = NEW.category_id LIMIT 1);
IF (update_price = 1) THEN
IF (NEW.is_bundle = 0) THEN
UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
ELSE IF (NEW.is_bundle = 1) THEN
UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
END IF;
END IF;
END IF;
END;
$$
DELIMITER ;
The current UPDATE statements are just placeholders for some actual calculations I'll end up doing.
Please note: I use Sequel Pro for most MySQL-related stuff and initially was using their GUI to try and add the trigger - it automatically adds the surrounding code so I would only create everything between the BEGIN and END statements. That also resulted in this same syntax error, so I don't believe it's related to the delimiters like some similar threads I've already found on here. Nevertheless, I've tried adding the full trigger code via a normal query; changing the delimiter syntax - for example END$$, END $$, END; $$ etc.
I've successfully created other triggers with similar syntax, but they do not include the DECLARE syntax.
Where am I going wrong?
The problem is here:
IF (NEW.is_bundle = 0) THEN
UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
ELSE IF (NEW.is_bundle = 1) THEN
UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
END IF;
Review documentation: https://dev.mysql.com/doc/refman/8.0/en/if.html
MySQL supports ELSEIF and this is different than ELSE IF. If you use ELSEIF, this continues the structure of the IF statement. If you use ELSE IF, it starts a new IF statement, so it should be like this:
IF (NEW.is_bundle = 0) THEN
UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
ELSE
IF (NEW.is_bundle = 1) THEN
UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
END IF;
END IF;
See that there is a complete IF/THEN/END IF statement within the ELSE block of the outer one?
But you didn't do that, so the END IF applies to the innermost IF statement, and then you're one level off for the rest of the body of the trigger.
When MySQL gets to the end of the whole CREATE TRIGGER statement, if there aren't enough ENDs to balance the blocks you began, MySQL complains with the error you saw.

how to use if condition in mysql with status = 2

create trigger with status with passed with status = 2 and I need to create a tigger after update row second time when del_status = 2
CREATE TRIGGER `exchange_log_update` AFTER UPDATE ON `exchange`
FOR EACH ROW INSERT
IF NEW.del_status = 2
INTO
exchange_log
SET
client_id = NEW.client_id,
ex_type = NEW.ex_type,
segment_type = NEW.segment_type,
validity_from = NEW.validity_from,
validity_to = NEW.validity_to,
file_upload = NEW.file_upload,
log_status = 'update',
created_at = NEW.created_at
END IF
END
#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 'IF NEW.del_status
INTO
exchange_log
SET
client_id = NEW.client_id,
ex' at line 3
try this
CREATE TRIGGER `exchange_log_update_new` AFTER UPDATE ON `exchange`
FOR EACH ROW BEGIN
IF (NEW.del_status = 2)
THEN
INSERT INTO `exchange_log`
values(client_id = NEW.client_id,
ex_type = NEW.ex_type,
segment_type = NEW.segment_type,
validity_from = NEW.validity_from,
validity_to = NEW.validity_to,
file_upload = NEW.file_upload,
log_status = 'update',
created_at = NEW.created_at);
END IF;
END

Mysql Trigger with IF and IF ELSE conditions

i have table 'comments' with fields 'type','receiver_id' ,'confirm' and 'id'.
there are three types of object that each have comment section.
after comment confirm the trigger increase the number of objects in their respective table.
DB::unprepared('CREATE TRIGGER comment_confirm
AFTER UPDATE ON comments FOR EACH ROW
BEGIN
IF OLD.confirm = 0 AND NEW.confirm = 1 THEN
IF OLD.type = profile THEN
UPDATE profiles SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = blog THEN
UPDATE blogs SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = topic THEN
UPDATE topics SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
END IF;
END IF;
END
');
the error for migration is:
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 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 13
Else if should be elseif (no space) IF OLD.type = profile doesn't look right should be in single quotes for a string comparison.

Mysql : Error in trigger

This is the trigger im trying to add. But here is the 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 'create trigger tgr_insert before insert on `nuss_order` for
each row ' at line 2 .
delimiter $$
drop trigger if exists tgr_insert
create trigger tgr_insert before insert on `nuss_order`
for each row
begin
declare a int;declare b varchar(14);declare d varchar(8);
set a = (select `id` from `nuss_order` where `id` = new.id);
set d = (select `date` from `nuss_order` where `date` = new.date);
if (a>=100) then
if(a>=1000) then
set b = 'BX' + d + cast(a as varchar);
else
b = 'BX' + d + concat('0',cast(a as varchar));
end if;
else
if(a>=10) then
b = 'BX' + d + concat('00',cast(a as varchar));
else
b = 'BX' + d + concat('000',cast(a as varchar));
end if;
end if;
set new.oid = b;
end
I can't find the error, if someone could help me it would be very nice. Thanks