Mysql存儲過程編寫 存儲過程編寫的模板: Create PROCEDURE PROCEDUREName (IN para mint,……) Begin Declare varname type; 語句; End; 以上就是存儲過程的編寫模板。 其中,type可以是表中的任意類型,比如:varchar,bigint,int,decimal,longtext等等類型。 游標的聲明是: Declare cursorName cursor from select語句。 Declare continue handler for not found set varName = 1; varName在使用的時候,需要進行聲明,這個是表明如果游標沒有數據了,varName賦值為1時表示沒有值。 Open cursorName;表示打開游標。 CLOSE cursorName;表示關閉游標。 FETCH cursorName into varlist;表示向游標中取出值。 If條件語句: 1、種情況 If 條件 then 滿足條件時執行的語句 End if; 2、種情況 If 條件 then 滿足條件的執行的語句 Else 不滿足條件的執行的語句 End if; 循環語句: Out_loop:LOOP
END LOOP out_loop; 這個是LOOP循環,其中out_loop表示的是LOOP的循環標簽,類似于匯編的標簽。 其中結束LOOP循環的語句是: LEAVE out_loop;out_loop表示LOOPd的標簽 例子: create PROCEDURE selectExtratUnit() BEGIN DECLARE id BIGINT; DECLARE na LONGTEXT; DECLARE linkName LONGTEXT; DECLARE notfound INT; DECLARE cursor_avgScore CURSOR for select summary_id,text2 from edoc_summary_extend_send_sj where text2 is not null and text2 <> ''; DECLARE CONTINUE HANDLER FOR NOT FOUND SET notfound = 1; OPEN cursor_avgScore; out_loop:LOOP if notfound = 1 THEN LEAVE out_loop; end if; FETCH cursor_avgScore into id,na; select group_concat(org_name) into linkName from trans_org_sj where org_id in ( SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(na,'|',help_topic_id+1),'|',-1) AS num FROM mysql.help_topic WHERE help_topic_id < LENGTH(na)-LENGTH(REPLACE(na,"|",''))+1 ); INSERT into extrat_table(id,orgname) VALUES(id,linkName); END LOOP out_loop; CLOSE cursor_avgScore; end;