儲存過程 游標 錯誤處理(重新生成使用者編號)

2022-05-13 20:36:05 字數 2949 閱讀 5849

--

set_account_data 重新生成使用者編號

begin

declare temp_id int(8); /*

使用者id

*/declare temp_manager int(8); /*

上級id

*/declare temp_accounter_no varchar(64); /*

上級編碼

*/declare temp_max_no varchar(64); /*

上級的最大下級編碼

*/declare max_no varchar(64); /*

編碼*/

declare str1 varchar(64); /*

編碼*/

declare temp_no int(8); /*

編碼*/

declare temp_level int(8); /*

級次*/

declare state varchar(30); /*

錯誤處理監聽變數

*//*

定義使用者表游標

*/declare account_cursor cursor

forselect id,manager from account order

bymanager,id;

/*定義錯誤處理監聽,用於結束游標迴圈

*/declare

continue handler for

1329

begin

set state =

'error';

end;

open

account_cursor;

repeat

fetch account_cursor into

temp_id,temp_manager;

if (temp_id =

1) then

update account set leaf =

0,no ='01

',level=1

where id =1;

else

/*設定上級leaf為0

*/update account set leaf =

0where id =

temp_manager;

/*查詢上級編號

*/select no into temp_accounter_no from account where id =

temp_manager;

/*設定上級編碼

*/update account set pno = temp_accounter_no where id =

temp_id;

/*查詢上級原有的最大下級編碼

*/select

max(no) into temp_max_no from account where pno =

temp_accounter_no;

/*如果最大下級編碼為空,生成新的編碼,否則把原來的編碼加一

*/if (temp_max_no is

null) then

set max_no = concat(temp_accounter_no, '

0001');

else

set str1 = substr(temp_max_no,length(temp_max_no)-

3,4);

set temp_no =

str1;

set temp_no = temp_no +1;

set str1 =

temp_no;

if (length(str1) =

1) then

set str1 = concat('

000'

, str1);

elseif (length(str1)

=2) then

set str1 = concat('00'

, str1);

elseif (length(str1)

=3) then

set str1 = concat('0'

, str1);

endif

;

set max_no =

concat(temp_accounter_no, str1);

endif

;

update account set no = max_no where id =

temp_id;

set temp_level = (length(max_no) +

2) /4;

update account set

level

= temp_level where id =

temp_id;

endif

; until state ='

error

'end

repeat;

close

account_cursor;

/*修改leaf為null的為1

*/update account set leaf =

1where leaf is

null

;

return0;

end

帶有事務和錯誤處理的儲存過程

1.建立錯誤日誌表create table dbo t errorlog errorlogid int identity 1,1 not null,errortime datetime not null,username sysname not null,errornumber int not nu...

ORA 28056錯誤處理過程

報錯內容 d sqlplus nolog sql plus release 11.1.0.7.0 production on 星期四 10月 8 20 03 50 2009 sql conn as sysdba error ora 28056 writing audit records to win...

MYSQL儲存過程游標定義出現錯誤

step 1 定義游標結束識別符號 declare done int default false step2 定義游標 declare dpid cur cursor forselect cm no from e c m db.data points step3 定義異常時改變游標結束識別符號機制 ...