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.
Related
I created a Before Insert MySQL trigger to check if few values already exists on the table, but the trigger is not working.
This is my trigger:
DELIMITER $$
CREATE TRIGGER TEST1
BEFORE INSERT
ON roles FOR EACH ROW
BEGIN
DECLARE atExists INT DEFAULT 0;
SELECT count(id) FROM roles WHERE personal_id = NEW.personal_id AND role = NEW.role AND favorite = New.favorite INTO atExists;
IF (atExists = 0) THEN INSERT INTO roles (personal_id, role, favorite) VALUES (NEW.personal_id, NEW.role, NEW.favorite);
END IF;
END $$;
DELIMITER;
I have a row that has for example values:
999999999, "admin", "abcde"
and when I try to do
INSERT INTO roles (personal_id, role, favorite) values (999999999, "admin", "abcde");
After running the trigger, it does adds the new row even though it should just skip it.
I tried with IF NOT EXISTS (...) etc, did not work.
I also checked the SELECT to see if it does find the existing rows and it does.
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 3 mysql tables:
user_followers has columns id,user_id
notification has columns id,user_id
notification_object has columns notification_id , type
On insert on user_followers i want to insert
user_id=New.user_id in notification
and then using the id of notification just inserted
notification_object.type = "2" and notification_object.notification_id = just inserted id in notifcication table
now the problem is how can i find the id of notifcation table:
DELIMITER //
CREATE TRIGGER follow_notification AFTER INSERT ON user_friends
FOR EACH ROW
Begin
insert into notification(user_id) values(New.user_id);
insert into notification_object(notification_id,type) values(????,"2");
End
//
if notifications.id is auto-incremented, mysql includes a function last_insert_id() that will return (with caveats) the lastly inserted id.
DELIMITER //
CREATE TRIGGER follow_notification AFTER INSERT ON user_friends
FOR EACH ROW
Begin
declare v_notification_id INT;
insert into notification(user_id) values(New.user_id);
/* set a variable to the last id inserted to the db */
set v_notification_id=(select last_insert_id());
/* use the variable in the next insert */
insert into notification_object(notification_id,type) values(v_notification_id,"2");
End//
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?
What i'm doing wrong?
CREATE TRIGGER `Calc` AFTER INSERT on `test`.`bookings` FOR EACH ROW
BEGIN
UPDATE `test`.`article` AS `ST`
SET `ST`.`stock` = SUM(`test`.`bookings`.`amount`)
WHERE `ST`.`articlenr` = `test`.`bookings`.`NEW.article`;
END
The idea is, to calculate "STOCK" in ARTICLE, after an insert like:
Amount: 1 Article: 123
on table bookings.
How can this be solved?
WHERE `ST`.`articlenr` = NEW.`article`;
New its not a column
=(SELECT SUM(`test`.`bookings`.`amount`) FROM `test`.`bookings` GROUP BY article)