MySQL 儲存過程和函式02
阿新 • • 發佈:2021-08-10
MySQL 儲存過程 4)case結構 方法一: case value when value then sql語句01 when value then sql語句02 else sql語句03 end case 方法二: case when 條件語句 then sql語句01 when 條件語句 then sql語句02 else sql語句03 end case --給定一個月份,然後計算出所在的季度 create procedure pro_test7(in month int) begin ...... end; ====================================================== create procedure pro_test7(month int) begin declare result varchar(10); case when month>=1 and month<=3 then set result='第一季度'; when month>=4 and month<=6 then set result='第二季度'; when month>=7 and month<=9 then set result='第三季度'; else set result='第四季度'; end case; select concat('傳遞的月份為:',month,'計算出的結果為:',result) as content; end; call pro_test7(5) 5)while迴圈 滿足條件繼續迴圈 while 條件語句 do --SQL語句 end while; --計算從1加到n的值 -- 累加 create procedure pro_test8(n int) begin declare total int default 0; declare num int default 1; while num<=n do set total = total + num; set num = num + 1; end while; select total; end; call pro_test8(5) drop procedure pro_test8; //刪除儲存過程 6)repeat迴圈 while 是滿足條件才執行迴圈,repeat是滿足條件就退出迴圈; repeat --SQL語句; until --SQL語句 //此處結束無分號 ; end repeat; --計算從1加到n的值------->repeat create procedure pro_test9(n int) begin declare total int default 0; repeat end repeat; end; ================================================ create procedure pro_test9(n int) begin declare total int default 0; repeat set total = total + n ; set n = n-1; until n = 0 end repeat; select total; end; call pro_test9(10); 7)loop迴圈 可實現簡單的死迴圈 退出迴圈的條件需要使用其他語句定義 如 leave語句 8)leave語句 --計算從1加到n的值------->loop....leave語句 create procedure pro_test10(n int) begin declare total int default 0; c:loop //loop迴圈 需要一個別名 c end loop c; select total; end; ==================================================================== create procedure pro_test10(n int) begin declare total int default 0; c:loop set total = total + n; set n = n - 1; if n <= 0 then leave c; end if; end loop c; select total; end; call pro_test10(4); 9)遊標|游標 遊標是用來儲存查詢結果集的資料型別 , 在儲存過程和函式中可以使用游標對結果集進行迴圈的處理。游標的使用 包括游標的宣告、OPEN、FETCH 和 CLOSE cursor---游標 遊標 fetch---請來 拿來 抓取 declare cursor_name cursor for --SQL語句; open sursor_name; fetch sursor_name into var_name(變數名) ......; close cursor_name; create table emp( id int(11) not null auto_increment , name varchar(50) not null comment '姓名', age int(11) comment '年齡', salary int(11) comment '薪水', primary key(`id`) )engine=innodb default charset=utf8 ; insert into emp(id,name,age,salary) values(null,'金毛獅王',55,3800),(null,'白眉鷹 王',60,4000),(null,'青翼蝠王',38,2800),(null,'紫衫龍王',42,1800); ----查詢emp表中資料,並逐行獲取 進行展示 create pro_test11() begin declare emp_result cursor for select * from emp; open emp_result; fetch emp_result into ......; close emp_result; end; ------------------------------------------------------ create procedure pro_test11() begin declare e_id int(11); declare e_name varchar(50); declare e_age int(11); declare e_salary int(11); declare emp_result cursor for select * from emp; open emp_result; fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id,'name=',e_name,'age=',e_age,'薪資為:',e_salary); fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id,'name=',e_name,'age=',e_age,'薪資為:',e_salary); fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id,'name=',e_name,'age=',e_age,'薪資為:',e_salary); fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id,'name=',e_name,'age=',e_age,'薪資為:',e_salary); close emp_result; end; call pro_test11(); drop procedure pro_test11(); -------------------------------------------------------------------------------- create procedure pro_test12() begin declare e_id int(11); declare e_name varchar(50); declare e_age int(11); declare e_salary int(11); declare has_data int default 1; declare emp_result cursor for select * from emp; DECLARE EXIT HANDLER FOR NOT FOUND set has_data=0; open emp_result; repeat fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id, 'name=', e_name, 'age=', e_age,'薪資為', e_salary); until has_data = 0 end repeat; close emp_result; end; call pro_test12(); DECLARE EXIT HANDLER FOR NOT FOUND set has_data=0; //mysql 控制代碼觸發退出機制 必須跟在遊標宣告下面 until has_data = 0 //此處後面結束符 不允許有分號 ; 10)儲存函式 有返回值的過程 create function function_name([引數列表....]) returns type begin ...... end; ------定義一個儲存函式,獲取滿足條件(city表)的總記錄數; create function fun1(countryID int) RETURNS int begin ...... end; ------------------------------------------------------------- create function fun1(countryID int) RETURNS int begin declare cnum int; select count(*) into cnum from city where country_id = countryID; return cnum; end; select fun1(2); drop function fun1;