creating trigger for Update other table - mysql

as a school project i'm making an order and storage system. The server should add the newly arrived items to the storage when the status of an order is changed to received. How do i do this? what i came up with until now is:
CREATE TRIGGER statuswijzigen BEFORE UPDATE
ON `voorraadorder`
FOR EACH ROW
BEGIN
IF NEW.Status = "ontvangen"
THEN UPDATE `voorraadartikel`
SET Voorraad = (SELECT voorraadartikel.Voorraad + voorraadordercontenct.Aantal
FROM `voorraadartikel` AS v
INNER JOIN `vooorraadordercontent` AS r ON v.VoorraadArtikelID = r.ContentID
INNER JOIN `voorraadorder` AS o ON r.VoorraadOrderID = o.VoorraadOrderID
WHERE v.VoorraadArtikelID = r.ContentID AND r.VoorraadOrderID = o.VoorraadOrderID);
END IF;
END
and the error i receive 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 '' at line 11
all help is welcome :)

Related

Trigger with update other table, from aggregation of another table

i'm try to make a trigger on MySql.
Cenario:
i've 3 tables:
com_proposals
com_proposals_items
com_proposals_subitems
IMPORTANT: the three tables has this columns (value_gross,value_disc_auto,value_disc_comercial)
And the source of this values is aways from the last one - com_proposals_subitems
If i updates qty in second table, i've to update the principal table (com_proposals)
And that needs the calculation(sum) of third table(..subitems).
Look:
DELIMITER $$
CREATE TRIGGER trigger_update_com_proposals_cart_items_updatevalues
AFTER UPDATE ON com_proposals_cart_items
FOR EACH ROW
BEGIN
SET #id = OLD.id;
SET #prop_id = OLD.proposal_id;
SELECT
#gross := sum(value_gross),
#auto := sum(value_disc_auto),
#com := sum(value_disc_comercial)
from
com_proposals_cart_subitems
where
cart_item_id = #id;
OLD.value_gross = #gross;
OLD.value_disc_auto = #auto;
OLD.value_disc_comercial = #com;
SELECT
#item_gross := sum(value_gross),
#item_auto := sum(value_disc_auto),
#item_com := sum(value_disc_comercial)
from
com_proposals_cart_items
where
id = #id;
UPDATE com_proposals set
value_gross = #item_gross,
value_disc_auto = #item_autoy,
value_disc_comercial = #item_com
WHERE id = #prop_id;
END$$
DELIMITER ;
But, i'm getting a syntax error:
Error occurred during SQL script execution
Razão:
SQL 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 '.value_gross = #gross;
OLD.value_disc_auto = #auto;
OLD.value_disc_comercial' at line 17
Someone kwons whats happining?? THANKS A LOT!!!
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

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.

Unidentifiable error in SQL syntax, thrown when trying to store a procedure

I am getting an error when I try to enter the following procedure using the phpMyAdmin SQL box. I'm pretty sure the syntax is correct though!
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
')
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = de' at line 1
SQL SYNTAX:
delimiter $$
CREATE PROCEDURE DeleteCard(IN aCard varChar)
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = decks.DeckName
SET decks.DeckTotal = decks.DeckTotal - amount.Amount
WHERE amount.CardName = aCard ;
UPDATE types t1
JOIN cards ON cards.TypeName = t1.TypeName
JOIN Amount ON amount.CardName = cards.CardName
SET t1.TypeTotal = t1.TypeTotal - amount.Amount
WHERE amount.CardName = aCard ;
DELETE
FROM amount
WHERE cardName = aCard;
DELETE
FROM cards
WHERE cardName = aCard;
END
$$
DELIMITER ;

Creating a trigger which enters value in studies(rollno,code) table only if total credits of a student are less than 30

I am getting the 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 '' at line 5.
CREATE TRIGGER db BEFORE INSERT ON studies
FOR EACH ROW
BEGIN
IF((select CAST(SUM(credits) as UNSIGNED) from ((SELECT credits from subject WHERE subject.code = NEW.code) union all (SELECT credits from subject,(SELECT code from studies where studies.rollno = NEW.rollno) as t1 WHERE subject.code = t1.code)) as t2) > 30) THEN
set NEW.rollno = NULL;
ENDIF;
END