We have a table which has IT incident tickets called ticket_data. I want when new tickets are inserted into if the criteria of if certain criteria is met (ticket logged today, logged by teams like 'MCO-TechServ%' and logged_via = 'Phone' then copy the row to the table ticket_data_sd_today
Im doing this as there are many reports which are reported on today's service desk tickets and I thought it would be easier to create a table with the day's tickets which gets purged daily.
I have tried to create a trigger to copy the row after insert but it's not working. Can anyone help? Thank you.
CREATE TRIGGER `sdinfo`.`todays_tickets`AFTER INSERT ON `ticket_data`
FOR EACH ROW
DELIMITER $$
BEGIN
IF (date(new.`logged_date_time`) = curdate()
and new.`logged_by_team` like 'MCO-TechServ%'
and new.`logged_via` = 'Phone') then
INSERT INTO ticketicket_data_sd_today
(`Ticket_Number,Logged_Date_Time,Call_Type,Call_Detail)
values (new.Ticket_Number,new.Logged_Date_Time,new.Call_Type,new.Call_Detail);
end if;
END $$
DELIMITER ;
Related
Firstly let me say I'm new to and just getting my head around MySQL and finding date manipulation has its challenges. It seems to me it should be possible to have three columns:
column A contains date member joined 2020-01-12 for example, mapped from a form.
column B contains the length of membership in years 1 or 5, currently entered manually
Then calculate expiry 'date A'+ 'integer B' Year inserted in column C on member creation or update
It's OK to do manually but I feel it should be something automatic.
If anyone can give me a start or point to a tutorial that might help I'd be grateful.
In MySql you can use DATE_ADD() funciton:
SELECT columnA, columnB, DATE_ADD(columnA, INTERVAL columnB YEAR) AS EXPIRY;
Use this web page as reference.
You could define triggers BEFORE INSERT and/or BEFORE UPDATE which will do the job for you:
BEFORE INSERT trigger:
DROP TRIGGER IF EXISTS before_insert;
DELIMITER $$
CREATE TRIGGER before_insert
BEFORE INSERT ON `my_table`
FOR EACH ROW
BEGIN
SET NEW.`valid` = DATE_ADD(NEW.`date`, INTERVAL NEW.`membership` YEAR);
END$$
DELIMITER ;
BEFORE UPDATE trigger:
DROP TRIGGER IF EXISTS before_update;
DELIMITER $$
CREATE TRIGGER before_update
BEFORE UPDATE ON `my_table`
FOR EACH ROW
BEGIN
SET NEW.`valid` = DATE_ADD(NEW.`date`, INTERVAL NEW.`membership` YEAR);
END$$
DELIMITER ;
It is quite enough to insert both date and membership values during insert (obviously) or to update membership value on already inserted record(s).
Your Column C is the valid column in both queries.
I'm learning JDBC and I'm trying to make a bank program. I'm using mySQL to make the database of the program which includes users and balance tables. I want to make a trigger that will automatically create data when data is inputted in the users table
So far i've made this code which doesn't work:
create trigger onInputUsers before insert on users
for each row
begin
insert into finances (id,balance,debt) values (null,0,0);
end;
NOTE: The table structure of your tables is not provided, hence there is an assumption that there are columns as like userid, balance and debt in your tables.
There can be two triggers that can fulfil the requirement of yours (one that creates a record whenever there is a new user created and the other when a transaction is made by the user)!
This handles the creation of the audit entries for your record, which is not very much required (if you ask for my opinion)
DELIMITER $$
CREATE TRIGGER USER_FINANCE_TRIGGER BEFORE INSERT ON users
FOR EACH ROW
BEGIN
INSERT INTO finances (id, userid, balance, debt) VALUES (null, OLD.userid, 0, 0);
END$$
DELIMITER;
This handles updating the finances table with the user activity:
DELIMITER $$
CREATE TRIGGER USER_FINANCE_UPDATE_TRIGGER BEFORE UPDATE ON balance
FOR EACH ROW
BEGIN
INSERT INTO finances (id, userid, balance, debt) VALUES (null, OLD.userid, OLD.balance, OLD.debt);
END$$
DELIMITER;
Hope this answers your question well!
After hours of pulling my hair out I could do with a little help with a MYSQL trigger. I have two tables running logs. Let's say a 'master' and a 'slave'. When a log is entered into the 'master', if a string within a column meets a certain criteria I need certain values from that new row to be copied to a slave table.
This is the closest I have gotten so far...
CREATE TRIGGER `web_log_filter` AFTER INSERT ON `master` FOR EACH ROW
BEGIN
IF (NEW.criteria_column = 'criteriaX') THEN
INSERT INTO slave (coulmn1,column2,column3)
VALUES(NEW.coulmn1,NEW.column2,NEW.column3);
END IF;
END
It is currently returning a syntax error on line 5 and can't figure out why.
Any help is much appreciated.
Are you using DELIMITER?
DELIMITER $$
CREATE TRIGGER `web_log_filter` AFTER INSERT ON `master`
FOR EACH ROW
BEGIN
IF (NEW.criteria_column = 'criteriaX') THEN
INSERT INTO slave (coulmn1, column2, column3)
VALUES(NEW.coulmn1, NEW.column2, NEW.column3);
END IF;
END;$$
DELIMITER ;
I need help creating a particular trigger using MySQL. The details for my trigger is given below along with my current statement. However, my statement keeps giving me an error message so I'm doing something wrong. Any assistance would be great! Thanks!
Problem a) Create a trigger called membership_balance_updates that will capture any updates made to the mem_balance column in membership. The trigger should only capture those transactions in which the member’s balance actually changes. The mem_num, old mem_balance, new mem_balance, user, transaction date should be placed into a membership_balance_audit table.
My Statement:
CREATE TRIGGER membership_balance_updates
AFTER UPDATE OF mem_balance ON membership
FOR EACH ROW
INSERT INTO membership_balance_audit
VALUES (mem_mum, old_mem_balance, new_mem_balance, transaction_date,
transaction_user);
Table "Membership"
Here's the following script to create the membership_balance_audit table.
CREATE TABLE IF NOT EXISTS membership_balance_audit
(mem_num INTEGER,
old_mem_balance DECIMAL(10,2),
new_mem_balance DECIMAL(10,2),
transaction_date TIMESTAMP,
transaction_user VARCHAR(50));
Part c
Try using this:
CREATE TRIGGER membership_balance_updates
AFTER UPDATE ON membership
FOR EACH ROW
BEGIN
IF NEW.mem_balance <> OLD.mem_balance THEN
INSERT INTO membership_balance_audit
(mem_mum, old_mem_balance, new_mem_balance, transaction_date,
transaction_user)
VALUES (OLD.membership_id, OLD.mem_balance, NEW.mem_balance, NOW(),
CONCAT(OLD.first_name, ' ', OLD.last_name));
END IF;
END;
MySql trigger is very interesting, but very tricky.
I have some problem, I want to run a trigger once after insert on .
I want to run my trigger once after rows inserted, is there anything like for each table``???
how to make this trigger run only once, but not for each row created.
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mydb`.`leave_taken_trigger`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
set #lt:= (SELECT count(*) FROM `leave` where (staff_leave_application_staff_id_staff = new.staff_leave_application_staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type) and active = 1 );
INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, taken_days, updated)
VALUES (new.leave_type_id_leave_type, new.staff_leave_application_staff_id_staff, IFNULL(#lt,0), CURDATE())
ON DUPLICATE KEY UPDATE taken_days=IFNULL(#lt,0), updated = CURDATE();
END$$
I think you can't achieve your requirement with trigger.
Your trigger will run in every row created.
I suggest you to using stored procedure from your code after the INSERT has successfully completed.