I am new to mysql and am trying to create a trigger. I want to take a newly inserted value from one table and want to update another table by subtracting this values from the value in that table.
CREATE TRIGGER inventory_updated
AFTER INSERT ON ORDERdetails
for EACH ROW
BEGIN
UPDATE product_trial
SET Quantity = Quantity-NEW.quantity
where productid = new.ProductID;
END
This gives SQL syntax error near line 6 (Update query) error 1064.
Kindly help.
In your example, you don't need BEGIN-END because there is a single query executed in the for loop. If you remove them if solves the syntax error.
If you need to keep the BEGIN-END because your real life case is more complex, then you need to define a delimiter, as
DELIMITER \\
CREATE TRIGGER inventory_updated AFTER INSERT ON ORDERdetails for EACH ROW
BEGIN
UPDATE product_trial SET Quantity = Quantity-NEW.quantity where productid = new.ProductID;
END \\
Related
I am trying to create triggers for my Oracle database and I always get compiled with errors when trying to add it.
The statement is as follows
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking SET open_amount = open_amount - NEW.amount WHERE Booking.booking_id = NEW.booking_id;
END;
and I get the following error
2/5 PL/SQL: SQL Statement ignored
2/90 PL/SQL: ORA-00904: "NEW"."BOOKING_ID": invalid identifier
The trigger should subtract the amount in the newly created payment row from the open_amount field in the booking row and store the new value.
I managed to get it to work in mysql with the following statement
DELIMITER $$
CREATE TRIGGER `update_open_amount` AFTER INSERT ON `Payment`
FOR EACH ROW
BEGIN
UPDATE `Booking` SET `open_amount` = `open_amount` - NEW.`amount` WHERE `Booking`.`booking_id` = NEW.`booking_id`;
END;
$$
DELIMITER ;
but when I try to get it to work in oracle I get stuck.
How can I get the trigger to work because this is all that is still missing for migrating the database from mysql to oracle.
How can I fix this?
Thanks in advance
Please check syntax for referring new and old values. It should have preceeding colon (:)
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking
SET open_amount = open_amount - :NEW.amount
WHERE Booking.booking_id = :NEW.booking_id;
END;
USE test;
CREATE TRIGGER AvgUpdateTrigger AFTER INSERT ON test.score
FOR EACH ROW
BEGIN
INSERT INTO test.average (test.average.TestID, test.average.TestAvg)
(SELECT test.score.TestID, avg(test.score.ScoreValue) FROM test.score GROUP BY test.score.TestID)
ON DUPLICATE KEY
UPDATE test.average.TestAvg = (SELECT avg(test.score.ScoreValue) FROM test.score WHERE test.score.TestID = test.average.TestID GROUP BY test.score.TestID);
END;
im trying to update one table(average) when another one gets changed(score)
it is telling me to add a semicolon but as you can see there is one there allready
If a trigger (or any stored procedure) contains only one statement, you don't need BEGIN and END:
CREATE TRIGGER AvgUpdateTrigger AFTER INSERT ON test.score
FOR EACH ROW
INSERT INTO test.average (TestID, TestAvg)
SELECT test.score.TestID, avg(test.score.ScoreValue)
FROM test.score
GROUP BY test.score.TestID
ON DUPLICATE KEY UPDATE
test.average.TestAvg = VALUES(TestAvg);
I also replaced the subquery with VALUES(TestAvg), since this value has already been selected.
I need some help with getting the syntax right to define a trigger in PHPmyAdmin.
What I have:
2 tables - invoice and invoiceitem
tbl_invoice has a field 'Total' which should show the sum of the 'Extended' price of the table invoiceitem
tbl_invoiceitem has 2 triggers that update the 'Extended' before_insert and before_update
What I try to accomplish:
Calculate the sum of all items for an invoice and update the 'Total' price in tbl_invoice after either new items are inserted or if the price or quantity for an existing item was updated.
I try to do this in PHPmyAdmin.
The error message is
I hope somebody can give mt a hint into the right direction. Just started with triggers and don't seem to find solution with a couple of days searching the internet.
Thank you for help in advance.
Cheers, Oliver
I can see 2 syntax errors in your code:
The delimiter commands should be outside of the trigger definition, not inside of it. Phpmyadmin should take care of this.
The commands within the begin ... end block should be terminated by semicolon (;), not by whatever you provide in the delimiter command.
I did not check if your sql commands within the trigger make sense, but the where condition of the set command does not seem right.
Ok, found a work around:write the full create trigger statement in a file and import with PHPmyAdmin. As 'Shadow' pointed out there was also a problem in the where conditions.
Below the corrected triggers that worked in my scenario.
There is a lot of examples on the net to get triggers done with the mysql client. This way you can use the exact syntax that you would type into a client and than import into PHPmyAdmin in case you can't reach the server your using with a command shell client.
Hope this helps other newbies.
Cheers, Oliver
-- after_insert trigger for InvoiceItem to Calculate the Total in Invoice
------------------------------------------------------------------------
DELIMITER //
CREATE TRIGGER `InvoiceItem_After_Insert` AFTER INSERT ON `invoiceItem`
FOR EACH ROW
BEGIN
SET #InvoiceNumber = NEW.InvoiceFK;
SET #ItemTotal = (SELECT SUM(Extended) FROM InvoiceItem WHERE InvoiceFk = #InvoiceNumber);
UPDATE Invoice SET Total = #ItemTotal WHERE id = #InvoiceNumber;
END
//
DELIMITER ;
-- after_update trigger for InvoiceItem to Calculate the Total in Invoice
------------------------------------------------------------------------
DELIMITER //
CREATE TRIGGER `InvoiceItem_After_Update` AFTER UPDATE ON `invoiceitem`
FOR EACH ROW
BEGIN
SET #InvoiceNumber = NEW.InvoiceFK;
SET #ItemTotal = (SELECT SUM(Extended) FROM InvoiceItem WHERE InvoiceFk = #InvoiceNumber);
UPDATE Invoice SET Total = #ItemTotal WHERE id = #InvoiceNumber;
END
//
DELIMITER ;
-- after_delete trigger for InvoiceItem to Calculate the Total in Invoice
----------------------------------------------------------------------
DELIMITER //
CREATE TRIGGER `InvoiceItem_After_Delete` AFTER DELETE ON `invoiceItem`
FOR EACH ROW
BEGIN
SET #InvoiceNumber = OLD.InvoiceFK;
SET #ItemTotal = (SELECT SUM(Extended) FROM InvoiceItem WHERE InvoiceFk = #InvoiceNumber);
UPDATE Invoice SET Total = #ItemTotal WHERE id = #InvoiceNumber;
END
//
DELIMITER ;
-----------------------------------------------------------------
The next 2 triggers are just single statement and are calculate the 'Extended' column in the InvoceItem table
---------------------------------------------------------------------
-- Calculate new 'Extended' price before insert
CREATE TRIGGER `InvoiceItem_Before_Insert` BEFORE INSERT ON `invoiceitem`
SET NEW.Extended = ROUND(NEW.Quantity * NEW.Price,2)
-------------------------------------------------------------------
-- Calculate new 'Extended' price before update
CREATE TRIGGER `InvoiceItem_Before_Update` BEFORE UPDATE ON `InvoiceItem`
FOR EACH ROW
SET NEW.Extended = ROUND(NEW.Quantity * NEW.Price,2);
I'm creating a trigger from the table ItemBorrower that is meant to update a field in the table Item. When ItemBorrower gets a new entry inserted, the currentDate field in the Item table should be updated to today's date. Here is my trigger:
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW UPDATE Item
BEGIN
SET Item.checkoutDate = CURDATE()
WHERE NEW.itemID = Item.itemID
END;
And there error I'm getting is about the last line:
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 'END' at line 8
I've played around with the delimeter (removing it, adding a ";" after Item.itemID), but to no apparent avail. Could be that I just did that totally wrong, too.
Any help appreciated!
Thanks,
Jona
I am not 100% sure what are you trying to accomplish from the trigger. I assumed that you have a table called item and you want to update the checkoutDate column with current date where the item.itemID = the last insert id in ItemBorrower.
Try this code
delimiter //
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW
BEGIN
UPDATE Item
SET checkoutDate = CURDATE()
WHERE itemID = NEW.itemID;
END; //
delimiter ;
I wrote a trigger something like this:
CREATE TRIGGER `update_after_itemPresent` AFTER INSERT ON `bus_repair`
FOR EACH ROW begin
IF NEW.unit <> `item_present`.`unit` THEN
update item_present
set unit = unit-new.unit
where item_present.item_group_id = new.item_group_id;
END IF;
end
But when I insert new row in bus_repair table it gives an error that:
unknown table item_present in field list
Any idea how to fix this?
Move your UPDATE item_preset statement above the IF and the IF inside the UPDATE or rephrase as a condition. You need to UPDATE or SELECT the table item_present first.