mysql 儲存過程 利用游標迴圈新增例項

2021-09-02 13:44:11 字數 2928 閱讀 9368

create definer = 'www'@'192`.`168`.`%`.`%'

procedure newsng.pro_test(in _params varchar(255))

begin

declare _string varchar(50);

declare _string1 varchar(50);

declare splitchar varchar(50);

declare p_name_index int;

declare p_id_index int;

declare p_class_id int;

declare stop int default 0;

declare p_name varchar(50);

declare p_clasid int;

declare p_tech_names varchar(50);

declare p_tech_name varchar(50);

declare p_tech_ids varchar(50);

declare p_tech_id int;

declare p_num int;##班級是否存在教師

declare i int default 0;

declare cur cursor for(

select clas_name, clas_id from classes where is_del = 0 );

declare exit handler for sqlexception, sqlwarning

begin

rollback; #回滾事務

end;

##declare continue handler for sqlstate '02000' set stop = null;

/*這把 游標 異常後 捕捉並設定 迴圈使用 變數 stop 為 null 跳出迴圈。*/

set splitchar = ',';

start transaction;

set @j = 0;

select count(1) into @j from classes;

select @j;

open cur;

##myloop: loop -- 開始迴圈體,myloop為自定義迴圈名,結束迴圈時用到

fetch cur into p_name, p_clasid;

while ( stop is not null) do

##查詢條件下的資料是否存在,避免游標迴圈中斷

select count(1) into p_num from teacher_class tc inner join edugate_base.teacher t on t.tech_id = tc.tech_id and t.is_del = 0 where tc.is_del = 0 and tc.clas_id = p_clasid;

if p_num = 0 then

select p_num;

else

select group_concat(t.tech_name) tech_name, group_concat(t.tech_id) tech_id, tc.clas_id into p_tech_names, p_tech_ids, p_class_id from teacher_class tc inner join edugate_base.teacher t on t.tech_id = tc.tech_id and t.is_del = 0 where tc.is_del = 0 and tc.clas_id = p_clasid group by tc.clas_id;

if p_tech_names = null then

select p_class_id;

else

set p_name_index = 1+(length(p_tech_names) - length(replace(p_tech_names, splitchar,'')));##教師名稱陣列長度

set @i = 0;

while @i < p_name_index

doset @i = @i + 1;

set p_tech_name = reverse(substring_index(reverse(substring_index(p_tech_names,splitchar,@i)),splitchar,1));## 如 11,12,13 先擷取第乙個逗號(,)之前的字串11 反序11 擷取第乙個逗號之前的11 再反序11,第二次擷取第二個逗號之前的字串11,12 反序21,11 擷取逗號之前的21, 再反序12

set p_tech_id = reverse(substring_index(reverse(substring_index(p_tech_ids,splitchar,@i)),splitchar,1));

insert into tmp_strs(name,tech_id,tech_name) values(p_name, p_tech_id, p_tech_name);

end while;

end if;

end if;

#if p_tech_ids = null then

# select p_class_id;

#else

# set p_id_index = 1+(length(p_tech_ids) - length(replace(p_tech_ids, splitchar,'')));##教師id陣列長度

#end if;

fetch cur into p_name, p_clasid;

end while;

commit;

##end loop myloop; -- 結束自定義迴圈體

close cur;

end

Mysql 儲存過程使用游標

完整例子 create procedure test begin 定義引數 declare id int 定義游標 declare no more products int default 0 declare result test cursor for select num1 from numte...

MySql儲存過程使用游標迴圈插入資料示例

本示例通過 while.end while 迴圈控制游標來實現插入表記錄。drop procedure if exists pro initcategoryfortradingentity create procedure pro initcategoryfortradingentity tradi...

Oracle使用游標迴圈呼叫儲存過程

宣告游標 cursor cursor name is select statement for 迴圈游標 1 定義游標 2 定義游標變數 3 使用for迴圈來使用這個游標 declare 型別定義 cursor c job is select a.workorderid from idc.pf or...