ERROR 1193 (HY000): Unknown system variable while Creating a trigger - mysql

Hi guys I intend to create a Trigger for MySQL table so that after Values are Inserted into the table, the Column exp_sales is set to qnty_received value *selling_price but I am getting this error:
ERROR 1193 (HY000): Unknown system variable 'exp_sales'
This is My Query:
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
set exp_sales = qnty_received * selling_price;
END$$
What is the problem and what is the best way to create the trigger so that after teh Quantity and selling Price is Inserted, the field for exp_sales(Total) is Updated with the right value?

If I unserstand corrctly exp_sales is a column name you want to update:
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
update Store_info_table
set exp_sales = NEW.qnty_received * NEW.selling_price
where id = NEW.id;
END$$
better approach would be to use INSERT INTO ON DUPLICATE KEY UPDATE
INSERT INTO table_name(...)VALUES(...)
ON DUPLICATE KEY SET exp_sales = qnty_received * selling_price;

If I understood you right you want to update a field of your record after insert. Try
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
begin
UPDATE Store_info_table
SET exp_sales = NEW.qnty_received * NEW.selling_price
WHERE id = NEW.id;
END;
$$

Haven't you forgotten about the NEW keyword?
Try this one -
SET NEW.exp_sales = NEW.qnty_received * NEW.selling_price;
But, I think you should not do this, because you always can calculate 'total' value in SELECT query.

Try the following code, it works for me
create trigger tsales
after insert
on
Store_info_table
for each row
set new.exp_sales = new.qnty_received * new.selling_price;
but since you want the exp_sales calculated automatically, why would you not use before instead of after?

Related

How to add a trigger in Oracle SQL to update a value after a row was created

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;

Create My sql trigger for After insert , update another table column using inserted record [duplicate]

What is the error in the following code. I am executing in mysql
CREATE TRIGGER tg_order_insert
BEFORE INSERT
ON `order` FOR EACH ROW
BEGIN
INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END;
Grocery is the database and order_seqid and order are 2 table.
order_seqid is a table with only 1 attribute if type int and auto increment.
Am trying to put a prefix on the id which we insert into order table.
I am getting 2 errors in INSERT INTO..... and END; line
Did you declare a delimiter before your trigger definition? Something like
DELIMITER //
CREATE TRIGGER tg_order_insert
BEFORE INSERT
ON `order` FOR EACH ROW
BEGIN
INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END
//
Because if you don't, then MySQL thinks you're trying to end your trigger definition when it sees that first ; and calls syntax error.

how to sum two columns and insert into total column automatically after every update

I have three columns column 1, column 2, column 3(total). i want to put the sum of column 1 and column 2 into column 3 after every update query..... note i am using code igniter.. how can i do it if i update col 1 then total column automatically update itself.
Try like this:
DELIMITER $$
CREATE TRIGGER updtrigger AFTER UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.col1 <> OLD.col1 OR NEW.col2 <> OLD.col2 THEN
SET NEW.col3 = NEW.col1 + New.col2;
END IF;
END $$
Here is a very good tutorial.
An AFTER UPDATE Trigger means that MySQL will fire this trigger after
the UPDATE operation is executed.
why dont you just update the db instead of creating a trigger. plus youve created the trigger after the insert, it should go before. but really you shouldn't be doing it that way
try this code:
DELIMITER $$
CREATE
TRIGGER `db_name`.`trigger_name` AFTER UPDATE
ON `db_name`.`table_name`
FOR EACH ROW BEGIN
UPDATE
`table_name`
SET
`column3` = `column1` + `column2`;
END$$
DELIMITER ;

mysql trigger to update the same table from another table

I'm trying to set up a trigger which will update same row by inserting some additional data from another table. Field receive.iccid is blank, and I want it to be updated on every insert. However this trigger doesn't work
delimiter //
CREATE TRIGGER ins_iccid
AFTER INSERT ON receive
FOR EACH ROW
BEGIN
UPDATE receive SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END//
delimiter ;
turns out that I need to use 'BEFORE INSERT' to achieve what i was trying to
CREATE TRIGGER `ins_iccid` BEFORE INSERT ON `receive`
FOR EACH ROW
BEGIN
SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END

MySQL trigger not working. What could be the issue?

I need to update an existing table after insert on another table. This is what I have.
DELIMITER $$
CREATE TRIGGER `some_trigger`
AFTER INSERT ON `old_table`
FOR EACH ROW BEGIN
UPDATE `new_table` set `some_column` = new.`column`
WHERE `new_table`.id = new.id
END $$
DELIMITER;
Trigger definition successfully executed and trigger exists with
SQL_MODE - NO_ENGINE_SUBSTITUTION
DEFINER - root#%
Is there something horribly wrong with this?
Can you please confirm that the new.id i.e the id generated on old_table insertion is also present in the new table ?
Because the possible reason could be that in new table new.id is not present at the time on old_table insertion.