if condition in trigger - mysql

i have a tow table name t1,t2 . Based on the value insert in t1 i want to insert in t2 .
i use the following trigger .
create trigger testt after insert on t1
BEGIN
for each row
if NEW.uid='Mill' then insert into t2 (uid2) values (NEW.uid)
end if
if NEW.uid='Farm' then insert into t2 (uid2) values (NEW.r)
end if
END
Please help me to resolve this issue . Thanks in advance

It looks like you just have some keywords out of order:
CREATE TRIGGER ...
FOR EACH ROW
BEGIN
...
END

create trigger testt after insert on t1
BEGIN
for each row BEGIN
if NEW.uid='Mill' then insert into t2 (uid2) values (NEW.uid)
end if
if NEW.uid='Farm' then insert into t2 (uid2) values (NEW.r)
end if
END

Related

MySQL - Insert Into 2 Tables on 1 Procedure

I need to create a procedure for inserting records into 2 tables, but on the second table, I want to insert the last ID that was inserted on the first table. Could anyone help me with this?
This is my query
DELIMITER //
DROP PROCEDURE IF EXISTS ROOM_FEATURE_INSERT;
CREATE PROCEDURE ROOM_FEATURE_INSERT (propID INT, featID INT, featNme VARCHAR(50))
BEGIN
-- BEGIN CHECK
IF NOT EXISTS
(
SELECT rFeatureName FROM COMPANY_T3s71.PROPERTY_RFEATURE PRFE
INNER JOIN COMPANY_T3s71.ROOM_FEATURE RFEA ON PRFE.rFeatureID=RFEA.rFeatureID
WHERE BINARY rFeatureName = featNme AND propertyID = propID
)
AND
(
SELECT rFeatureName FROM COMPANY_T3s71.ROOM_VIEW
WHERE BINARY rFeatureName = featNme
)
THEN
-- IF NOT EXISTS INSERT INTO 1st TABLE
INSERT INTO COMPANY_T3s71.ROOM_FEATURE (rFeatureName) VALUES (featNme);
END IF;
-- END CHECK
-- BEGIN CHECK 2nd TABLE
IF NOT EXISTS
(
SELECT propertyID, rFeatureID FROM COMPANY_T3s71.PROPERTY_RFEATURE
WHERE rFeatureID = featID AND propertyID = propID
)
THEN
-- IF NOT EXISTS INSERT INTO 2nd TABLE
INSERT INTO COMPANY_T3s71.PROPERTY_RFEATURE (propertyID, rFeatureID) VALUES (propID, featID);
END IF;
-- END CHECK 2nd TABLE
END
DELIMITER ;
How do we pass the featID param, when we just inserted it on the first INSERT query?
Thank you before hand.
Use SET featID = LAST_INSERT_ID(); after the first query and then use the variable
INSERT INTO COMPANY_T3s71.ROOM_FEATURE (rFeatureName) VALUES (featNme);
SET featID = LAST_INSERT_ID();
However, if the data is not insert at anytime then you have to make query in the if block to set the value for featID.

After Insert trigger on table1 with Update IF condition on table2

I want to create trigger 'after insert' on table1
If i insert record in Table1
it will check corresponding ID in table2 and update the status of
corresponding ID in table2.
there is additional condition on table2.
status of that ID should be Null.
my attempt so far.
but its not working
CREATE TRIGGER 'table1_AFTER_INSERT` AFTER INSERT ON `table1` FOR EACH ROW
BEGIN
update table2 a
set a.status= 'coordination pass',
where a.ID = new.ID and a.status is Null;
END
You have many sintax errors like comma after set a.status= 'coordination pass', or in trigger name definition etc. Here is the correct triger sintax
CREATE TRIGGER `table1_AFTER_INSERT` AFTER INSERT ON `table1`
FOR EACH ROW BEGIN
UPDATE `table2` a
SET a.status= 'coordination pass'
WHERE a.ID = NEW.ID AND a.status is NULL;
END
As i seen your trigger there is two error first is with table name you use ' single quote and second is set a.status= 'coordination pass', comma at last
CREATE TRIGGER table1_AFTER_INSERT
AFTER INSERT ON table1`
FOR EACH ROW
BEGIN
UPDATE table2 a
SET a.status= 'coordination pass'
WHERE a.ID = new.ID AND a.status is Null;
END;

"after update" trigger fires after insert in mySQL

I have 2 triggers on my table as defined...
DELIMITER $$
CRATE TRIGGER newRecordToHistory AFTER INSERT ON myTable FOR EACH ROW
BEGIN
IF (new.recordType = 1) THEN
INSERT INTO myTableHistory
(
myTableId,
someInformation,
reason,
mytimestamp,
status
)
VALUES
(
new.myTableId,
new.someInformation,
new.reason,
now(),
'NEW'
);
END IF;
END$$
DELIMITER ;
and
DELIMITER $$
CRATE TRIGGER updateRecordToHistory AFTER UPDATE ON myTable FOR EACH ROW
BEGIN
IF (new.recordType = 1) THEN
INSERT INTO myTableHistory
(
myTableId,
someinformation,
reason,
mytimestamp,
status
)
VALUES
(
new.myTableId,
new.someInformation,
new.reason,
now(),
'UPDATED'
);
END IF;
END$$
DELIMITER ;
When I insert a new record into myTable, I get 2 records in myHistoryTable...
ID someInformation reason mytimestamp status
1 'This is new' 'Needed new record' 09/12/14 08:00:00 'NEW'
1 'This is new' 'Needed new record' 09/12/14 08:00:00 'UPDATED'
I also get a record in the table when I delete. But my code handles inserting into the history table when I delete (so the user can specify the reason for deleting it) from myTable.
I would only expect only one record in myHistoryTable on insert and no extra records in the myHistoryTtable when I delete (other than the ones I put there manually). Why does this happen? And how can I avoid this?
Thanks!

mysql trigger error with 2 conditions

I want to add an after insert trigger which will do the following.
The first IF condition works normally, but when it comes to the second everything stops.
Any ideas?
USE `Syslog`;
DELIMITER $$
CREATE TRIGGER `SystemEventsR_AINS` AFTER INSERT ON SystemEventsR FOR EACH ROW
IF
(exists
(select syslogtag from SystemEventsRcounter where syslogtag=
new.syslogtag)
AND
(select simpledate from SystemEventsRcounter
where syslogtag=new.syslogtag)=new.simpledate)
THEN
UPDATE SystemEventsRcounter
SET records=records+1
WHERE SystemEventsRcounter.syslogtag=new.syslogtag;
ELSE INSERT SystemEventsRcounter (simpledate, syslogtag, records) values (new.simpledate,new.syslogtag,1);
END IF
UPDATED:
What you need is INSERT INTO ... ON DUPLICATE KEY.
CREATE TRIGGER `SystemEventsR_AINS`
AFTER INSERT ON SystemEventsR
FOR EACH ROW
INSERT INTO SystemEventsRcounter (simpledate, syslogtag, records)
VALUES (NEW.simpledate, NEW.syslogtag, 1)
ON DUPLICATE KEY UPDATE records = records + 1;
In order for it to work you need to create a unique composite index on (simpledate, syslogtag)
CREATE UNIQUE INDEX idx_u_simpledate_syslogtag
ON SystemEventsRcounter (simpledate, syslogtag);
Here is SQLFiddle demo.
If you wanted it your way then it might look like
DELIMITER $$
CREATE TRIGGER `SystemEventsR_AINS`
AFTER INSERT ON SystemEventsR
FOR EACH ROW
BEGIN
IF (
SELECT COUNT(*) simpledate
FROM SystemEventsRcounter
WHERE syslogtag = NEW.syslogtag
AND simpledate = NEW.simpledate
) > 0 THEN
UPDATE SystemEventsRcounter
SET records = records + 1
WHERE SystemEventsRcounter.syslogtag = NEW.syslogtag;
ELSE
INSERT INTO SystemEventsRcounter (simpledate, syslogtag, records)
VALUES (NEW.simpledate, NEW.syslogtag, 1);
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo.

MySQL Trigger to insert data into different DB

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