Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code: 1422. Explicit or implicit commit is not allowed in stored function or trigger

Everywhere I look it seems MySQL stored procedures can do transactions. Yet when I declare my stored function

create function test( a int )
returns int
MODIFIES SQL DATA
BEGIN
  START TRANSACTION ;
  update t set col='some value' where id=a ;
  COMMIT ;
  return 0 ;
END //

I get

Error Code: 1422. Explicit or implicit commit is not allowed in stored function or trigger.

like image 806
bobobobo Avatar asked Jun 06 '13 18:06

bobobobo


1 Answers

Actually you are not allowed transactions inside stored functions. You are allowed transactions inside stored procedures only.

create procedure test( a int )
MODIFIES SQL DATA
BEGIN
  START TRANSACTION ;
  update t set col='some value' where id=a ;
  COMMIT ;
END //

To return values from the SP, use output parameters or use the result set from the last select statement in the SP.

like image 142
bobobobo Avatar answered Nov 17 '22 20:11

bobobobo