mysql存儲過程loop
declare storeId varchar(10);
在存儲過程中創建游標,這個游標裡面存了你所有要循環的數據,集合:
declare diy_cursor cursor for
select store_id from t_b_store; 
open diy_cursor;--打開游標
diy_loop:loop ---這里開始循環
FETCH diy_cursor into storeId; --提取本次循環的數據,保存在storeId中
if done = 1 then --done是在存儲過程開始的時候定義的一個整形變數
leave diy_loop;---如果游標中的數據提取完畢,就自動跳出這個循環end if;
----在這里用你循環取到的storeId做你想做的事情,就是寫你的sql啦---
end loop; --循環結束
         close diy_loop; --關閉游標
2. mysql數據存儲過程
MySQL字元串連接使用CONCAT函數,示例如下:

3. mysql存儲過程 游標雙重循環
在老版本的MySQL 3.22中,MySQL的單表限大小為4GB,當時的MySQL的存儲引擎還是ISAM存儲引擎。但是,當出現MyISAM存儲引擎之後,也就是從MySQL 3.23開始,MySQL單表最大限制就已經擴大到了64PB了(官方文檔顯示)。也就是說,從目前的技術環境來看,MySQL資料庫的MyISAM存儲 引擎單表大小限制已經不是有MySQL資料庫本身來決定,而是由所在主機的OS上面的文件系統來決定了。
而MySQL另外一個最流行的存儲引擎之一Innodb存儲數據的策略是分為兩種的,一種是共享表空間存儲方式,還有一種是獨享表空間存儲方式。
當使用共享表空間存儲方式的時候,Innodb的所有數據保存在一個單獨的表空間裡面,而這個表空間可以由很多個文件組成,一個表可以跨多個文件存在,所 以其大小限制不再是文件大小的限制,而是其自身的限制。從Innodb的官方文檔中可以看到,其表空間的最大限制為64TB,也就是說,Innodb的單 表限制基本上也在64TB左右了,當然這個大小是包括這個表的所有索引等其他相關數據。
而當使用獨享表空間來存放Innodb的表的時候,每個表的數據以一個單獨的文件來存放,這個時候的單表限制,又變成文件系統的大小限制了。
4. MySQL 存儲過程 怎麼實現 循環sql語句
delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc() //創建while循環的存儲過程 if分支語句示例
-> BEGIN
->
->     DECLARE i int;
->     SET i=1;
->     loop1: WHILE i<=10 DO
->          IF MOD(i,2)<>0 THEN /*Even number - try again*/
->             SELECT CONCAT(i," is an odd number");
->          END IF;
->          SET i=i+1;
->     END WHILE loop1;
-> END$$
Query OK, 0 rows affected (0.00 sec)
這種也可以
5. 五、MYSQL存儲過程和函數
 • create procere用來創建  存儲過程  ,create function用來創建  函數  
   
 •   Delimiter命令是改變語句的結束符  ,MySQL默認的結束符為;號,由於procere和function中的;號並不代表創建的結束,所以要替換成另外的結束符以便表示創建的結束
   • rontine_body子句可以包含一個簡單的SQL語句,也可以包含多個SQL語句,  通過begin…end將這多個SQL語句  包含在一起
   • MySQL存儲過程和函數中也可以包含類似create和drop等DDL語句
   • comment子句用來寫入對存儲過程和函數的注釋
   •   Language子句用來表示此存儲過程和函數的創建語言  
   •   存儲過程和函數被標注為deterministic表明當輸入相同的參數是會返回相同的結果,反之如果是not deterministic則表示相同參數不會是相同結果,默認是not deterministic  
• 相關屬性短語只有咨詢含義,並不是強制性的約束
 • Drop procere/function語句用來  刪除指定名稱的存儲過程或函數  
   
 • Begin…end語句通常出現在存儲過程、函數和觸發器中,其中  可以包含一個或多個語句  ,每個語句用;號隔開
   
 • 標簽label可以加在begin…end語句以及loop, repeat和while語句
   •   語句中通過iterate和leave來控制流程,iterate表示返回指定標簽位置,leave表示跳出標簽  
   
 •   Declare語句通常用來聲明本地變數、游標、條件或者handler  
   •   Declare語句只允許出現在begin … end語句中而且必須出現在第一行  
   •   Declare的順序也有要求,通常是先聲明本地變數,再是游標,然後是條件和handler  
 • 本地變數可以通過declare語句進行聲明
   •   聲明後的變數可以通過select … into var_list進行賦值,或者通過set語句賦值,或者通過定義游標並使用fetch … into var_list賦值  
   • 通過declare聲明變數方法:
   
• MySQL支持if,case,iterate,leave,loop,while,repeat語句作為存儲過程和函數中的 流程式控制制語句 ,另外return語句也是函數中的特定流程式控制制語句
 • Case語句在存儲過程或函數中表明了  復雜的條件選擇語句  
   
 • IF語句在存儲過程或函數中表明了  基礎的條件選擇語句  
   
 其中在 function 裡面,只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。如果我們開啟了 bin-log, 我們就必須為我們的 function 指定一個參數。
   在 MySQL 中創建函數時出現這種錯誤的解決方法:
    set global log_bin_trust_function_creators=TRUE; 
 • Iterate語句  僅出現在loop,repeat,while循環語句中,其含義表示重新開始此循環  
   
 • Leave語句表明  退出指定標簽的流程式控制制語句塊  
   • 通常會用在begin…end,以及loop,repeat,while的循環語句中
   
 • Loop語句是存儲過程或函數中表達  循環執行  的一種方式
   
 • repeat語句是存儲過程或函數中表達  循環執行  的一種方式
   
 • while語句是存儲過程或函數中表達  循環執行  的一種方式
   
 • Return語句用在  函數中,用來終結函數的執行並將指定值返回給調用者  
   
 • Cursor游標用來  聲明一個數據集  
   • 游標的聲明必須在變數和條件聲明之後,在handler聲明之前
 • Cursor close語句用來  關閉之前打開的游標  
   
 • Cursor declare語句用來聲明一個游標和指定游標對應的數據集合,  通常數據集合是一個select語句  
   
 • Cursor fetch語句用來獲取游標指定數據集的  下一行數據  並將各個欄位值賦予後面的變數
   
 • Open cursor語句用來打開一個之前已經  聲明好的游標  
   
 • Declare condition語句命名  特定的錯誤條件  ,而該特定錯誤可以在declare…handler中指定  處理方法  
   
 • 比如在MySQL中1051error code表示的是unknown table的錯誤,如果要對這
   個錯誤做特殊處理,可以用三種方法:
 • Declare handler語句用來聲明一個handler來處理一個或多個特殊條件,當其中的某個條件滿足時則觸發其中的statement語句執行
   • Statement可以是一個簡單SQL語句,也可以是begin…end組成的多個語句
• Handler_action子句聲明當執行完statement語句之後應該怎麼辦
Condition_value的值有以下幾種:
• 當condition發生但沒有聲明handler時,則存儲過程和函數依照如下規則處理
 • create trigger語句用來創建一個觸發器,觸發器的作用是當表上有對應SQL語句發生時,則觸發執行
   • 觸發器創建時需要  指定對應的表名  tbl_name
 •   Definer關鍵詞用來指定trigger的安全環境  
   • Trigger_time指定觸發器的執行時間,BEFORE和AFTER指定觸發器在表中的  每行數據修改前或者後  執行
   • Trigger_event指定觸發該觸發器的具體  事件  
   • INSERT當新的一行數據插入表中時觸發,比如通過執行insert,load data,replace語句插入新數據
   • UPDATE當表的一行數據被修改時觸發,比如執行update語句時
   • DELETE當表的一行數據被刪除時觸發,比如執行delete,replace語句時
   • 當執行insert into … on plicate key update語句時,當碰到重復行執行update時,則觸發update下的觸發器
   • 從5.7.2版本開始,可以創建具有相同trigger_time和trigger_event的同一個表上的多個觸發器,默認情況下按照創建的時間依次執行,通過  指定FOLLOWS/PRECEDES改變執行順序  ,即FOLLOWS時表示新創建的觸發器後執行,PRECEDES則表示新觸發器先執行
   • Trigger_body表示觸發器觸發之後要執行的一個或多個語句,在內部可以引用涉及表的欄位,  OLD.col_name表示行數據被修改或刪除之前的欄位數據,NEW.col_name表示行數據被插入或修改之後的欄位數據  
• Drop trigger語句用來 刪除一個觸發器
 • If exists短語用來避免刪除不存在的觸發器時引發報錯
   •   當你執行drop table時,表上的觸發器也被drop掉了  
6. mysql存儲過程中分支語句有哪些
存儲過程:
create procere p()
begin
 /*thi  procere does nothing*/
end;
1.參數
Parameters 參數
讓我們更進一步的研究怎麼在存儲過程中定義參數1. CREATE PROCEDURE p5
() ...
2. CREATE PROCEDURE p5
([IN] name data-type) ...
3. CREATE PROCEDURE p5
(OUT name data-type) ...
4. CREATE PROCEDURE p5
(INOUT name data-type) ...
2.Conditions and if-then-else 條件式和 if-then-else
3.Loops 循環語句
WHILE ... END WHILE
LOOP ... END LOOP
REPEAT ... END REPEAT
GOTO
4.DECLARE HANDLER syntax 聲明異常處理的語法
DECLARE
{ EXIT | CONTINUE }
HANDLER FOR
{ error-number | { SQLSTATE error-string } | condition }
SQL statement
5.Cursors 游標
游標實現功能摘要:
DECLARE cursor-name CURSOR FOR SELECT ...;
OPEN cursor-name;
FETCH cursor-name INTO variable [, variable];
CLOSE cursor-name;
已現經在可我以們完開成始基著本眼的游事標了務如。聲雖明然游我標們,的打存開儲游過標程,中從的游游標標里語法讀取還,並關沒閉有完游整標。
6.Functions 函數
Summary:
摘要 CREATE FUNCTION
Limitations of functions
函數的限制
我們已經很清楚可以在存儲過程中使用的元素了。下面我要講的是前面沒有提到的函數。
7. mysql 存儲過程
.關於MySQL的存儲過程
存儲過程是資料庫存儲的一個重要的功能,但是MySQL在5.0以前並不支持存儲過程,這使得MySQL在應用上大打折扣。好在MySQL 5.0終於開始已經支持存儲過程,這樣即可以大大提高資料庫的處理速度,同時也可以提高資料庫編程的靈活性。
MySQL存儲過程的創建
(1).格式
MySQL存儲過程創建的格式:CREATE PROCEDURE過程名([過程參數[,...]])
[特性...]過程體
這里先舉個例子:
- mysql>DELIMITER// 
- mysql>CREATEPROCEDUREproc1(OUTsint) 
- ->BEGIN 
- ->SELECTCOUNT(*)INTOsFROMuser; 
- ->END 
- ->// 
- mysql>DELIMITER; 
- 註:
(1)這里需要注意的是DELIMITER //和DELIMITER ;兩句,DELIMITER是分割符的意思,因為MySQL默認以";"為分隔符,如果我們沒有聲明分割符,那麼編譯器會把存儲過程當成SQL語句進行處理,則存儲過程的編譯過程會報錯,所以要事先用DELIMITER關鍵字申明當前段分隔符,這樣MySQL才會將";"當做存儲過程中的代碼,不會執行這些代碼,用完了之後要把分隔符還原。
(2)存儲過程根據需要可能會有輸入、輸出、輸入輸出參數,這里有一個輸出參數s,類型是int型,如果有多個參數用","分割開。
(3)過程體的開始與結束使用BEGIN與END進行標識。
8. mysql存儲過程該怎麼做
看懂了 你所說的需求並不難 
begin
declare tikk datetime ; 
declare done int default 0; 
declare userid int default 0; 
declare moleid int default 0; 
declare couid int default 0;
declare mname varchar(255) ; 
declare opsid int default 0; 
declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status<>0 group by I_userID,I_operationID 
createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moleID from space_operation where status<>0 and ID=opsid into moleid;
if moleid <> '' then 
select Nvc_identification from space_operation where status<>0 and ID=opsid into @identiftion;
if moleid > 0 then
Select Nvc_ename from space_mole where status<>0 and ID=moleid into mname;
else
set mname = 'space';
end if; 
create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status<>0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;
set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID > @ihod 
ID asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1); 
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1); 
end if; 
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1); 
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;
if @content0 <> '' then
set @contentp = CONCAT(@operarry1,concat('|',@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid 
createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));
end if;
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1; 
end if;
end if;
until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;
UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID > @uoj order by ID asc limit 0,1;
if @io1k <> '' then 
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while; 
drop temporary table if exists sp_tab2 ; 
end if;
end
