mysql 儲存過程 游標完成批處理

2021-09-10 13:11:33 字數 1674 閱讀 9362

本例作用:配置線入參為空,則將當前使用者維護所有配置線關係。配置線入參非空,則維護當前使用者配置線關係。

delimiter $$  -- 自定義結束符

drop procedure if exists insert_user_branch_ralation; -- 刪除已有的儲存過程

create procedure insert_user_branch_ralation(in cmupid int,in branchid int) -- 建立儲存過程 兩個入參 使用者id和配置線id

begin

declare s int default 0; -- 宣告四個變數

declare cmbpid int; -- 配置線id

declare bracode varchar(15); -- 配置線碼值(本例中沒有用到)

declare braname varchar(15);-- 配置線名稱(本例中沒有用到)

declare branch cursor for select a.cmb_pid,a.sub_branch_code,a.sub_branch_name from cgp_mng_branch a ; -- 建立游標並將查詢結果放入branch游標中

declare continue handler for sqlstate '02000' set s=1; -- 如果游標中沒有值了 則將變數s設定為1

if branchid is not null then -- 入參為空則先刪後增

delete from cgp_mng_userbra where cmu_pid = cmupid and cmb_pid = branchid;

insert into cgp_mng_userbra(cmu_pid,cmb_pid,createdby,createdtime,validity) values(cmupid,branchid,'admin',now(),'1');

else -- 入參為空 則刪除當前使用者所有配置線 在維護上所有配置線

delete from cgp_mng_userbra where cmu_pid = cmupid;

open branch; -- 開啟游標

fetch branch into cmbpid,bracode,braname; -- 將游標中的值取出來放到上邊宣告的變數裡邊

while s<>1 do -- 如果s不等於1 就執行迴圈

insert into cgp_mng_userbra(cmu_pid,cmb_pid,createdby,createdtime,validity) values(cmupid,cmbpid,'admin',now(),'1');

fetch branch into cmbpid,bracode,braname; -- 再在迴圈中將游標中的值傳入到變數中

end while ;

-- 關閉游標

close branch;

end if;

end$$

delimiter ; -- 將結束符還原為分號

call insert_user_branch_ralation(3,1);  -- 給使用者3維護1配置線

call insert_user_branch_ralation(2,null); -- 給使用者2維護所有配置線

mysql 游標 儲存過程

1.首先需要注意的是mysql中游標必須要建立在儲存過程中 2.直接上sql 查詢當天資料 select in flow out flow from water meter data where 1 1 and date sys read time curdate 1 order by in flo...

MySQL 儲存過程 游標

儲存過程 本儲存過程有特殊執行迴圈數量的要求,是對security market history表進行修正 判斷儲存過程是否存在 drop procedure if exists proc security market history update create procedure proc se...

mysql 儲存過程 游標

宣告游標 declare cursor name cursor for select statement 這個語句宣告乙個游標。也可以在子程式中定義多個游標,但是乙個塊中的每乙個游標必須有唯一的名字。開啟游標 open cursor name 這個語句開啟先前宣告的游標。游標fetch fetch ...