mysql 遞迴呼叫
背景:
表結構為上下級關係,想批量更新乙個字段。
過程:
在寫儲存過程的時候,temp_id當初寫的是使用者變數
set @temp_id=0
執行結果自增這個變數始終沒生效,但當我去掉遞迴的時候,一層一層呼叫,就是正確的。
解決方式:
將 temp_id 換成區域性變數就行了
declare temp_id int(10) default 0;
猜測可能原因:
在儲存過程中呼叫函式或者是其他儲存過程,會使使用者變數失效
create
definer
=`root`
@`%`
procedure
`test`
(in inid varchar(32
),in scode varchar(15
))begin
declare temp_id int(10
)default0;
-- 必須是區域性變數,不能設定成使用者變數
declare cache_id varchar(32
)default'';
declare temp_ordernum decimal(6
);declare done integer
default0;
declare cur_award cursor
forselect
distinct id,priority from test_table where
if(inid='',
`parent_id`
isnull
,parent_id=inid)
order
by priority;
declare
continue
handler
for sqlstate '02000'
set done=1;
set @@max_sp_recursion_depth=10
;-- 遞迴深度
set temp_id=1;
open cur_award;
fetch cur_award into cache_id,temp_ordernum;
while
(done=0)
doset
@show_code
=temp_id;
update test_table set test_table.show_cod=
@show_code
where id=cache_id;
set temp_id=temp_id+1;
call test(cache_id,
@show_code);
fetch cur_award into cache_id,temp_ordernum;
endwhile
;close cur_award;
end;
MySQL儲存過程遞迴呼叫
有分類表tb system category,結構如下 create table tb system category id int 11 not null auto increment,c parent id int 11 not null,c name varchar 50 not null,c...
MySQL呼叫儲存過程
使用儲存過程,可以使程式執行效率更高,安全性更好,增強程式的可重用性和維護性 儲存過程有多種呼叫方法 儲存過程必須使用call語句呼叫,並且,儲存過程和資料庫相關,如果,要執行其他資料庫中的儲存過程,需要指定資料庫名稱 語法格式 call sp name parameter sp name,為儲存過...
mysql 儲存過程批量更新
最近做乙個批量更新的操作,由於是臨時需求,就想著在資料庫直接操作,不在 裡動手了,結合網上的一些資料,做如下處理 1.先建立乙個臨時表,匯入需要變動的資料 drop table if exists t barcode create table t barcode barcode varchar 32...