2013-06-20 58 views
0

當表用戶刪除失敗時,爲什麼下一個sql會優於。誰可以給我一個完整的mysql例程程序。我想刪除example.please幫助嗎?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 ; 

回答

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 ; 

我沒有嘗試的語法,但它應該工作。 ;-)

+0

我嘗試你的代碼,但它也刪除表user_info; –

+0

@ medinaok,你的代碼如果t_error!= 0那麼是錯誤的,這是如果t_error = 0 then.thank你很糊塗! –

+0

您是否調試過您在@t_error中獲得的內容?只需添加一個SELECT @t_error;你會看到在提示 – medina