MySql中捕獲異常的方法

2022-02-28 18:17:49 字數 1098 閱讀 3961

mysql中是否能有sqlserver的@@error變數呢,或者如c#中的try catch語法呢。

答案是肯定的,例項**如下:

code

drop

procedure

ifexists

sp_call_jobs;

create

procedure

sp_call_jobs()

notdeterministic

sql security definer

comment 

''begin

declare

_row,_err,_

count

intdefault0;

declare

continue

handler 

forsqlexception,sqlwarning,

notfound 

set_err=1

;while

_row

<3do

start 

transaction

;insert

into

t1(cond_val)

values

(null

);commit;if

_err=1

then

set_

count=_

count+1

;endif;

set_row

=_row+1

;end

while

;select

_count

;end

;語句:

declare continue  handler for sqlexception,sqlwarning,not found set _err=1;

作用是當遇到sqlexception,sqlwarning,not found 錯誤時,設定_err=1並執行continue操作,即繼續執行後面的語句。

這就與c#中的try catch語法很像。

而且在執行可能出錯的語句的時候我們用事務語句:start transaction; …… commit; 可以保證完整性。

mysql異常捕獲 MySql中捕獲異常的方法

下面是程式設計之家 jb51.cc 通過網路收集整理的 片段。mysql中是否能有sqlserver的 error變數呢,或者如c 中的try catch語法呢。答案是肯定的,例項 如下 code drop procedure if exists sp call jobs create proced...

MySql中捕獲異常的方法

mysql中是否能有sqlserver的 error變數呢,或者如c 中的try catch語法呢。答案是肯定的,例項 如下 drop procedure ifexists sp call jobs create procedure sp call jobs notdeterministic sql...

mysql異常捕獲 mysql異常處理

error 1130 hy000 host 172.17.0.1 is not allowed to connect to this mysql server use mysql update user set host where user root flush privileges 解決辦法 進...