mysql的存儲過程可以很方便使用游標來實現(xiàn)一些功能,存儲過程的寫法大致如下:
先創(chuàng)建一張表,插入一些測試數(shù)據(jù):
DROP?TABLE?IF?EXISTS?netingcn_proc_test; CREATE?TABLE?`netingcn_proc_test`?( ??`id`?INTEGER(11)?NOT?NULL?AUTO_INCREMENT, ??`name`?VARCHAR(20), ??`password`?VARCHAR(20), ??PRIMARY?KEY?(`id`) )ENGINE=InnoDB; insert?into?netingcn_proc_test(name,?password)?values ('procedure1',?'pass1'), ('procedure2',?'pass2'), ('procedure3',?'pass3'), ('procedure4',?'pass4');
下面就是一個簡單存儲過程的例子:
drop?procedure?IF?EXISTS?test_proc; delimiter?// create?procedure?test_proc() begin --?聲明一個標志done,?用來判斷游標是否遍歷完成 DECLARE?done?INT?DEFAULT?0; --?聲明一個變量,用來存放從游標中提取的數(shù)據(jù) --?特別注意這里的名字不能與由游標中使用的列明相同,否則得到的數(shù)據(jù)都是NULL DECLARE?tname?varchar(50)?DEFAULT?NULL; DECLARE?tpass?varchar(50)?DEFAULT?NULL; --?聲明游標對應(yīng)的?SQL?語句 DECLARE?cur?CURSOR?FOR select?name,?password?from?netingcn_proc_test; --?在游標循環(huán)到最后會將?done?設(shè)置為?1 DECLARE?CONTINUE?HANDLER?FOR?NOT?FOUND?SET?done?=?1; --?執(zhí)行查詢 open?cur; --?遍歷游標每一行 REPEAT --?把一行的信息存放在對應(yīng)的變量中 FETCH?cur?INTO?tname,?tpass; if?not?done?then --?這里就可以使用?tname,?tpass?對應(yīng)的信息了 select?tname,?tpass; end?if; ? UNTIL?done?END?REPEAT; CLOSE?cur; end // delimiter?; --?執(zhí)行存儲過程 call?test_proc();
需要注意的是變量的聲明、游標的聲明和HANDLER聲明的順序不能搞錯,必須是先聲明變量,再申明游標,最后聲明HANDLER。上述存儲過程的例子中只使用了一個游標,那么如果要使用兩個或者更多游標怎么辦,其實很簡單,可以這么說,一個怎么用兩個就是怎么用的。例子如下:
drop?procedure?IF?EXISTS?test_proc_1; delimiter?// create?procedure?test_proc_1() begin DECLARE?done?INT?DEFAULT?0; DECLARE?tid?int(11)?DEFAULT?0; DECLARE?tname?varchar(50)?DEFAULT?NULL; DECLARE?tpass?varchar(50)?DEFAULT?NULL; DECLARE?cur_1?CURSOR?FOR select?name,?password?from?netingcn_proc_test; DECLARE?cur_2?CURSOR?FOR select?id,?name?from?netingcn_proc_test; DECLARE?CONTINUE?HANDLER?FOR?NOT?FOUND?SET?done?=?1; open?cur_1; REPEAT FETCH?cur_1?INTO?tname,?tpass; if?not?done?then select?tname,?tpass; end?if; ? UNTIL?done?END?REPEAT; CLOSE?cur_1; --?注意這里,一定要重置done的值為?0 set?done?=?0; open?cur_2; REPEAT FETCH?cur_2?INTO?tid,?tname; if?not?done?then select?tid,?tname; end?if; ? UNTIL?done?END?REPEAT; CLOSE?cur_2; end // delimiter?; call?test_proc_1();
上述代碼和第一個例子中基本一樣,就是多了一個游標聲明和遍歷游標。這里需要注意的是,在遍歷第二個游標前使用了set done = 0,因為當?shù)谝粋€游標遍歷玩后其值被handler設(shè)置為1了,如果不用set把它設(shè)置為 0 ,那么第二個游標就不會遍歷了。當然好習慣是在每個打開游標的操作前都用該語句,確保游標能真正遍歷。當然還可以使用begin語句塊嵌套的方式來處理多個游標,例如:
drop?procedure?IF?EXISTS?test_proc_2; delimiter?// create?procedure?test_proc_2() begin DECLARE?done?INT?DEFAULT?0; DECLARE?tname?varchar(50)?DEFAULT?NULL; DECLARE?tpass?varchar(50)?DEFAULT?NULL; DECLARE?cur_1?CURSOR?FOR select?name,?password?from?netingcn_proc_test; DECLARE?cur_2?CURSOR?FOR select?id,?name?from?netingcn_proc_test; DECLARE?CONTINUE?HANDLER?FOR?NOT?FOUND?SET?done?=?1; open?cur_1; REPEAT FETCH?cur_1?INTO?tname,?tpass; if?not?done?then select?tname,?tpass; end?if; ? UNTIL?done?END?REPEAT; CLOSE?cur_1; begin DECLARE?done?INT?DEFAULT?0; DECLARE?tid?int(11)?DEFAULT?0; DECLARE?tname?varchar(50)?DEFAULT?NULL; DECLARE?cur_2?CURSOR?FOR select?id,?name?from?netingcn_proc_test; DECLARE?CONTINUE?HANDLER?FOR?NOT?FOUND?SET?done?=?1; open?cur_2; REPEAT FETCH?cur_2?INTO?tid,?tname; if?not?done?then select?tid,?tname; end?if; ? UNTIL?done?END?REPEAT; CLOSE?cur_2; end; end // delimiter?; call?test_proc_2();