MySQL After Upadte

I have written a trigger (with help from EE)
CREATE DEFINER=`root`@`%` TRIGGER trg_WalletIssue
    AFTER UPDATE ON tblpermit
    FOR EACH ROW 
	BEGIN
	IF New.CostWallet > 1 Then
    INSERT INTO tblservice_log
	
    SET      	DDAI_ServicesID = '4',
			id = New.id,
			UserID = (SELECT b.UserID FROM tblBatch b WHERE b.BatchID = New.PermitBatchID LIMIT 1),
			DDAI_Services_Date = NOW();
	END IF;
END

Open in new window


My problem is that if any part of the record is updated the trigger will execute, this is causing multiple records to be inserted into the service log.

How can I only execute the trigger only if the CostWallet changes and is >1
BrogrimInformation Systems Development ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
Hi.

Try amending the IF statement to use multiple conditions with NEW.CostWallet <> OLD.CostWallet as one of the conditions.

In other words:
CREATE DEFINER=`root`@`%` TRIGGER trg_WalletIssue
    AFTER UPDATE ON tblpermit
    FOR EACH ROW 
	BEGIN
	IF (NEW.CostWallet <> OLD.CostWallet AND NEW.CostWallet > 1) Then
    INSERT INTO tblservice_log
	
    SET      	DDAI_ServicesID = '4',
			id = NEW.id,
			UserID = (SELECT b.UserID FROM tblBatch b WHERE b.BatchID = NEW.PermitBatchID LIMIT 1),
			DDAI_Services_Date = NOW();
	END IF;
END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrogrimInformation Systems Development ManagerAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.