I am trying to create a live update in a history table. Whenever record is inserted/updated in o_training approval table , a corresponding record should be INSERTED in o_training_appr_history table. For this i created below triggers.
appr_after_insert
**DELIMITER $$
create trigger olattest.appr_after_insert after insert on o_training_approval
for each row
BEGIN
INSERT INTO o_training_appr_history
select
NEW.request_id,
NEW.user_id,
m.band,
m.grade,
NEW.approver_id,
NEW.approver_role,
NEW.project_id,
NEW.course_id,
NEW.comments,
NEW.submitted_date,
NEW.status_id,
m.vertical,
m.sub_vertical,
m.city,
m.location_type as location
from o_training_approval t join o_master_employees m
on(t.user_id = m.emp_id)
where m.emp_id=NEW.user_id;
END;
$$**
appr_after_update
**DELIMITER $$
create trigger olattest.appr_after_update after update on o_training_approval
for each row
BEGIN
INSERT INTO o_training_appr_history
select
NEW.request_id,
NEW.user_id,
m.band,
m.grade,
NEW.approver_id,
NEW.approver_role,
NEW.project_id,
NEW.course_id,
NEW.comments,
NEW.submitted_date,
NEW.status_id,
m.vertical,
m.sub_vertical,
m.city,
m.location_type as location
from o_training_approval t join o_master_employees m
on(t.user_id = m.emp_id)
where m.emp_id=NEW.user_id;
END;
$$**
Both the triggers got created without any error. However, on insert/update on the base table , no record is getting inserted in the history table.
I am clueless as for the reason and solution. Could anyone please help in this regard?
Related
I am creating a MySQL database, I want to create a trigger to change the amount of inventory available after a sale, but I get an error
This is the code of the trigger
DROP TRIGGER IF EXISTS tr_inventario;
CREATE TRIGGER tr_inventario AFTER INSERT ON factura FOR EACH ROW
UPDATE producto INNER JOIN factura ON producto.IdProducto=factura.IdProducto
SET producto.Invenatario=producto.Invenatario-factura.Cantidad
WHERE producto.IdProducto=factura.IdProducto;
This is the INSERT that i want to do
INSERT INTO factura(FechaVenta, NombreCliente, IdProducto, IdEmpleado, Cantidad) VALUES (now(), 'MarĂa Guadalupe',3, 2, 2);
And this is the error that I get
Error Code: 1048. Column 'Invenatario' cannot be null
relational table
You don't need a JOIN:
DELIMITER $$
CREATE TRIGGER tr_inventario AFTER INSERT ON factura FOR EACH ROW
BEGIN
UPDATE producto p
SET p.Invenatario = p.Invenatario - new.Cantidad
WHERE p.IdProducto = new.IdProducto;
END;
I have two tables in mysql db, one is account master table and the another one is account transaction table. On insert/update/delete on the transaction table I have to update the available balance and the last transaction date in the account master table ( the account master table contains more than 1 account). Is it possible with a trigger?
I have tried with the following Trigger. But trigger is not getting executed, getting syntax error(MSG 1064 LINE 30 MY SQL DB ERROR).
Please help to resolve if this can be handled through a trigger.
DELIMITER $$
CREATE TRIGGER wlt_bal_upd_insert AFTER INSERT ON wallet_txns
FOR EACH ROW
BEGIN
UPDATE wallet_accounts
SET wlt_bal_available = select sum(IF(wlt_txn_type = 'Expense', -wlt_txn_amount, wlt_txn_amount))from wallet_txns where wlt_name = new.wlt_name,wlt_last_txn_date = select MAX(wlt_txn_date)from wallet_txns where wlt_name = NEW.wlt_name
WHERE wlt_holder_id = NEW.wlt_holder_id
and wlt_name = new.wlt_name;
END $$
DELIMITER ;
I just forgot to put the brackets (). Its working now. Here is the modified code.
DELIMITER $$
CREATE TRIGGER wlt_bal_upd_insert AFTER INSERT ON wallet_txns
FOR EACH ROW
BEGIN
UPDATE wallet_accounts
SET wlt_bal_available = (select sum(IF(wlt_txn_type = 'Expense', -wlt_txn_amount, wlt_txn_amount))from wallet_txns where wlt_name = new.wlt_name),wlt_last_txn_date = (select MAX(wlt_txn_date)from wallet_txns where wlt_name = NEW.wlt_name)
WHERE wlt_holder_id = NEW.wlt_holder_id
and wlt_name = new.wlt_name;
END $$
DELIMITER ;
In the code below I'm trying to create a trigger which inserts 'incidentimg' table id along with an image path if the user of the web application did not upload a image with the form belonging to 'incidentreport' table, however, I'm only getting the error "MySQL said: #1422 - Explicit or implicit commit is not allowed in stored function or trigger"
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
IF(SELECT incidentimg.incidentImgId FROM incidentimg
LEFT JOIN incidentreport ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL)
THEN INSERT INTO incidentimg(incidentImgId, imgUrl)values(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
;
Here are the tables being used
Can someone assist me on how to correct this problem
I finally got the code to work. Now the code will check whether 'incidentimg' table has all the 'incidentReportId' that the 'incidentreport' table has, and if it doesn't then the last inserted id from the 'incidentreport' table along with a default image path will be inserted into 'incidentimg' table AFTER INSERT.
CREATE TRIGGER insertImage AFTER INSERT ON incidentreport
FOR EACH ROW
BEGIN
IF(SELECT incidentreport.incidentReportId
FROM incidentreport
LEFT JOIN incidentimg ON incidentreport.incidentReportId = incidentimg.imgs_incidentReportId
WHERE incidentimg.imgs_incidentReportId IS NULL)
THEN INSERT INTO incidentimg(imgs_incidentReportId, imgUrl) VALUES(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
I am not much sure but I am guessing that probably it's because of the IF .. THEN conditional INSERT. Try modifying your INSERT statement with an EXISTS like
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
INSERT INTO incidentimg(incidentImgId, imgUrl)
SELECT NEW.incidentReportId, 'images/no-image.png'
FROM DUAL WHERE EXISTS (
SELECT 1 FROM incidentimg
LEFT JOIN incidentreport
ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL
)
END;
I have a requirement where i have to create a trigger when there is an update in "psoprdefn" table for specific roles from "psroleuser" and insert values into my customized table.
For some reason the below trigger does'nt seem to work. Please help.
CREATE TRIGGER z_LOGIN2
AFTER UPDATE OF lastsignondttm ON SYSADM.PSOPRDEFN
FOR EACH ROW
BEGIN
SELECT DISTINCT a.oprid, a.oprdefndesc, a.lastsignondttm, b.rolename
from SYSADM.psoprdefn a, SYSADM.psroleuser b
where a.oprid=b.roleuser
and (rolename = 'FAS Change Admin' or b.rolename='PeopleSoft Administrator')
INSERT INTO z_LOGIN VALUES(psoprdefn.oprid,psoprdefn.oprdefndesc,psoprdefn.lastlogondttm)
Try something like this:
CREATE TRIGGER z_LOGIN2 AFTER UPDATE OF lastsignondttm ON SYSADM.PSOPRDEFN
FOR EACH ROW
BEGIN
IF (SELECT COUNT(1)
FROM SYSADM.psroleuser b
WHERE b.roleuser = NEW.oprid AND (b.rolename = 'FAS Change Admin'
OR b.rolename = 'PeopleSoft Administrator')) > 0 THEN
INSERT INTO z_LOGIN VALUES (NEW.oprid,NEW.oprdefndesc,NEW.lastlogondttm);
END IF;
END;
This validates if the user that updated the original table has a rolename of 'FAS Change Admin' or 'PeopleSoft Administrator' in psroleuser. If he does, it inserts the updated values in the z_LOGIN table.
It sounds so simple... I spent a few hours just getting the first part working which was a mysql trigger to a different database. Now I want to get smart and JOIN a couple tables.
I have two master tables PROJ and COMP. Both share id. When PROJ gets inserted I want to insert some of the NEW.PROJ info and some of the COMP info into a single row in the db.table axis.axis_data
Would someone please help me do a SELECT...INSERT with a TRIGGER. I might be in over my head on this one
My WORKING Trigger.
DELIMITER $$
DROP TRIGGER IF EXISTS `rate_data_trigger` $$
CREATE TRIGGER rate_data_trigger
BEFORE INSERT on PROJ FOR EACH ROW
BEGIN
INSERT INTO axis.axis_data
(projinfo_table_id, rate_user, name,
property_owner, property_address, property_city,
property_state, property_zip, property_phone,
rating_date, rating_type, rating_reason, rating_number
)
VALUES
(NEW.id, user(), NEW.BLGNAME,
NEW.POWNER, NEW.STREET, NEW.CITY,
NEW.STATE, NEW.ZIP, NEW.PHONE,
NEW.RATDATE, NEW.RATTYPE, NEW.RATREAS, NEW.RATNGNO
);
END$$
DELIMITER ;
Simply use the following syntax in your select statement:
INSERT INTO axis.axis_data
(projinfo_table_id, rate_user, name,
property_owner, property_address, property_city,
property_state, property_zip, property_phone,
rating_date, rating_type, rating_reason, rating_number,
field1, field2
)
SELECT NEW.id, user(), NEW.BLGNAME,
NEW.POWNER, NEW.STREET, NEW.CITY,
NEW.STATE, NEW.ZIP, NEW.PHONE,
NEW.RATDATE, NEW.RATTYPE, NEW.RATREAS, NEW.RATNGNO,
c.field1, c.field2
FROM COMP c WHERE c.id = NEW.id
If COMP doesn't always have a corresponding record in PROJ, you can do use SELECT ... FROM DUAL LEFT JOIN COMP c ON c.id = NEW.id