Live with Dot Net Just another Programming weblog

Below is a MySql trigger to insert/update other table

Posted on January 8, 2012

Trigger in backend is very useful to report the input data in many different way.. This will be last step in performance tuning when the data double up drastically.

DROP trigger IF EXISTS "Trigger Name";

delimiter $$
CREATE TRIGGER "Trigger Name"
AFTER INSERT ON "Table Name - where the trigger will be fired on Insert/Update"
FOR EACH ROW

BEGIN

Declare l_count, l_topErrorCount INT;
Declare l_Top1Error, l_Top2Error, l_Top3Error VARCHAR(150);

//To determine where you need to update or insert the value in other table
SELECT COUNT(*) INTO l_topErrorCount FROM "Table Name"
WHERE Team = NEW.Team
AND Date = NEW.ErrorDate
AND Report = NEW.Report;

////
You Logic goes here.....
//Retrive Top Error...//
////
IF l_topErrorCount > 0 THEN
UPDATE "Table Name"
SET Top1Error= l_Top1Error,
Top2Error=l_Top2Error,
Top3Error=l_Top3Error
WHERE Team = NEW.Team
AND Date = NEW.ErrorDate
AND Report = NEW.Report;
ELSE
INSERT INTO "Table Name"
(Date, Team, Report, Top1Error, Top2Error, Top3Error)
VALUES(NEW.ErrorDate, NEW.Team, NEW.Report, l_Top1Error, l_Top2Error, l_Top3Error);
END IF;

END;
$$

About admin

No description. Please complete your profile.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


ERROR: That reCAPTCHA response was incorrect.

 

No trackbacks yet.