0

当表用户删除失败时,为什么下一个sql会被执行。谁能给我一个完整的mysql过程示例。我想删除示例。请帮忙?

use test;
    delimiter //
    create procedure proc_name(in paramter int)
    begin
        declare t_error int default 0;
        declare continue handler for sqlexception set t_error=1;
        set autocommit = 0;
        START TRANSACTION;
        delete from `user` where id = paramter;
        delete from `user_info` where uid = paramter;

        if t_error=1 then
             rollback; 
        else
             commit;     
        end if;
    end;
    //
    delimiter ;
4

1 回答 1

0

Dinel,我会改变你的一些逻辑来实现你正在寻找的东西:

use test;

delimiter //
create procedure proc_name(in paramter int)
begin
    declare continue handler for sqlexception set t_error=1;
    SET @t_error = 0;

    START TRANSACTION;
    delete from `user` where id = paramter;
    SELECT @t_error := COUNT(*) from `user` where id = paramter;

    if @t_error != 0 then
         ROLLBACK; 
    else
        DELETE FROM `user_info` WHERE uid = paramter;
        COMMIT;
    end if;
end;
//
delimiter ;

我没有尝试语法,但它应该可以工作。;-)

于 2013-06-20T06:38:47.517 回答