Trigger with update other table, from aggregation of another table - mysql

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)

Related

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 ;

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 ;

Keep Getting a Syntax Error when Creating MySQL Trigger

I am trying to Create a Trigger that will fire AFTER Insert of a Record where I will see if there is other records similar to this Inserted Record (Same Date) and if so will update a column in the inserted Record. Once I complete this one I will also update it for AFTER Update as well. Any Help would be Greatly Appreciated.
CREATE
TRIGGER `INSERT_POSTDATEINDEX` AFTER INSERT
ON `zoomloca_listings-dev`.`listings_posts`
FOR EACH ROW
BEGIN
DECLARE vNewPostDateIndex INT;
DECLARE vLastPostDateIndex INT DEFAULT '0';
SET vNewPostDateIndex = '0';
SET vLastPostDateIndex = (SELECT POSTDATEINDEX FROM listings_posts WHERE date(POST_DATE) = date(NEW.POST_DATE) ORDER BY POSTDATEINDEX DESC LIMIT 1);
IF vLastPostDateIndex = '0' THEN
SET vNewPostDateIndex = '0';
ELSE
SET vNewPostDateIndex = vLastPostDateIndex + 1;
END IF;
Update `listings_posts` SET POSTDATEINDEX = vNewPostDateIndex where ID = New.ID;
END
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 6
The problem is that there is no TOP in MySQL. You have to use LIMIT instead. Besides, if you are not using mysql client, then you should remove DELIMITER since it is not a feature of the MySQL. Another thing is that to demarcate the end of an IF statement in MySQL, you should use END IF instead of ENDIF.

MySQL Trigger not saving

Executing this:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END
Results in this:
/* SQL 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 8 */
What am I missing? This should work, shouldn't it?
Thanks
You're most likely trying to add the trigger without changing the delimiter. Since the trigger contains semicolon, you'll have to change the delimiter to something else temporarily;
DELIMITER //
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END //
DELIMITER ;
An SQLfiddle with the trigger adding successfully. Note that the delimiter is changed in the settings,
Here's another implementation:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;

creating trigger for Update other table

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 :)