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

2021-07-24 06:52:15 字數 1687 閱讀 4045

本示例通過 while...end while 迴圈控制游標來實現插入表記錄。

drop procedure if exists pro_initcategoryfortradingentity;

create procedure pro_initcategoryfortradingentity(tradingentityid int)

begin

declare f_parent,entityid  int;

declare b int default 0;    /*是否達到記錄的末尾控制變數*/

declare f_name varchar(100);   

declare cur_1 cursor for select fname,fparent,tradingentityid from t_category_tag;

declare continue handler for not found set b = 1;

open cur_1;

fetch cur_1 into f_name, f_parent, entityid; /*獲取第一條記錄*/

while b<>1 do           

insert into t_category(fname,fparent,ftradingentity)  values(f_name, f_parent, entityid);

fetch cur_1 into f_name, f_parent, entityid; /*取下一條記錄*/

end while;

close cur_1;       

end;

call pro_initcategoryfortradingentity(2);

另外迴圈也可以用repeat...end repeat 來迴圈控制,以下是我最初所做的示例**,但結果是插入的實際資料總數多出一條,在此列出作為乙個反面示例**。當然最後用的是上面while...end while 方案。尚優旺

drop procedure if exists pro_initcategoryfortradingentity;

create procedure pro_initcategoryfortradingentity(tradingentityid int)

begin

declare f_parent,entityid,b  int;

declare f_name varchar(100);

declare cur_1 cursor for select fname,fparent,tradingentityid from t_category_tag;

declare continue handler for not found

set b = 1;

open cur_1;

repeat

fetch cur_1 into f_name, f_parent, entityid;

insert into t_category(fname,fparent,ftradingentity)  values(f_name, f_parent, entityid);

until b = 1

end repeat;

close cur_1;       

end;

call pro_initcategoryfortradingentity(2);

Mysql 儲存過程使用游標

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

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

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

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

create definer www 192 168 procedure newsng.pro test in params varchar 255 begin declare string varchar 50 declare string1 varchar 50 declare splitcha...