Solved

MySQL: Rollback Insert through BEFORE Triggers

Posted on 2008-10-05
3
3,538 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now