Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL: Rollback Insert through BEFORE Triggers

Posted on 2008-10-05
3
Medium Priority
?
3,723 Views
Last Modified: 2012-05-05
Hi,

I'd like to rollback a Insert statement is my check in the BEFORE INSERT trigger don't satisfy a condition ...

I read somewhere that any exception during the trigger would prevent the insert, but I don't know how throw this exceptions ...

Can someone help me ?

Thanks,
0
Comment
Question by:marcelocbf
  • 2
3 Comments
 
LVL 5

Expert Comment

by:libin_v
ID: 22645895
raise_application_error(-10001,'Error in Insert');
0
 
LVL 5

Accepted Solution

by:
libin_v earned 2000 total points
ID: 22645916
Sorry, I was using a UDF.. U can use a stored procdedure

DROP PROCEDURE IF EXISTS `MyRaiseError`$$

CREATE PROCEDURE `MyRaiseError`(msg VARCHAR(62))
BEGIN
DECLARE Tmsg VARCHAR(80);
SET Tmsg = msg;
IF (CHAR_LENGTH(TRIM(Tmsg)) = 0 OR Tmsg IS NULL) THEN
SET Tmsg = 'ERROR GENERADO';
END IF;
SET Tmsg = CONCAT('@@MyError', Tmsg, '@@MyError');
SET @MyError = CONCAT('INSERT INTO', Tmsg);
PREPARE stmt FROM @MyError;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$

Usage:
call MyRaiseError('Here error message!');
0
 

Author Comment

by:marcelocbf
ID: 22699695
Sorry for the delay ...
I'll study your solution ... Thanks
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question