mysql存儲過程repeat
MySQL 存儲過程中,使用游標查詢,返回的是結果集時,如何查看調用存儲過程輸出結果呢?
解決方案:存儲過程不返回數據,但它能創建和填充另一個表。所以在存儲過程運行中創建臨時表。該臨時表將保存存儲過程中生成的結果集,在遍歷游標時,用insert保存每條數據到臨時表中。後續調用時可以用select語句查詢臨時表中的存儲過程運行結果。
以下有 三種方式 使用游標創建一個存儲過程,統計某一部門下的員工信息
方法一:Loop循環
調用存儲過程:
方法二:While 循環
調用存儲過程:
方法三:REPEAT 循環
調用存儲過程:
上述三種實現方法在測試過程中遇到下述問題。
調用存儲過程查詢臨時表輸出結果時,會發現多循環了一次,像這樣:
解決方法:
在遍歷游標查詢結果時,先判斷游標的結束標志(done) 是否是為1,如果不是1,則向臨時表中插入數據。
Ⅱ mysql存儲過程游標結果集時,數據沒有遍歷完整
CREATEDEFINER=`root`@`%`PROCEDURE`insertPresale`()
BEGIN
#Routinebodygoeshere...
DECLAREdoneINTDEFAULT0;/*用於判斷是否結束循環*/
DECLAREgoodsIdVARCHAR(255);#標記商品id
DECLAREflagINTDEFAULT0;#標記資料庫是否包含此條商品記錄
/*用於存儲結果集的記錄*/
/*定義游標*/
_idFROM`sys_goods_publish`WHEREpresale=1ANDpresale_time<=NOW();
/*定義設置循環結束標識done值怎麼改變的邏輯*/
=1;/*done=true;亦可*/
OPENidCur;/*打開游標*/
/*循環開始*/
REPEAT
#/*如果要fetch多列應該這樣寫,fetchcur/*對應下面的idCur*/
FETCHidCurINTOgoodsId;/*還可以fetch多列(假設結果集的記錄不是單列的話)*/
IFNOTdoneTHEN/*數值為非0,MySQL認為是true*/
SELECTCOUNT(*)INTOflagFROM`itemsinfonew`WHERETaoBaoitemId=goodsId;
IF(flag>0)THEN#如果資料庫中有爬取此條記錄則刪除已用戶發布的為准
DELETEFROM`itemsinfonew`WHERETaoBaoitemId=goodsId;
ENDIF;
INSERTINTO`itemsinfonew`(TaoBaoitemId,CouponID,CreateSourceName)
SELECTgoods_id,coupon_id,SUBSTRING("customer_fd",0.5)
FROM`sys_goods_publish`
WHEREpresale_time<=NOW()ANDgoods_id=goodsId;
UPDATE`sys_goods_publish`SETpresale=0WHEREpresale=1ANDpresale_time<=NOW()ANDgoods_id=goodsId;
ENDIF;
UNTILdoneENDREPEAT;
CLOSEidCur;/*關閉游標*/
Ⅲ mysql存儲過程求大神(拼接字元串)
DROPPROCEDUREIFEXISTS`p_create_view`
CREATEPROCEDUREp_create_view()
BEGIN
DECLAREtableNameVARCHAR(100);
DECLAREstopFlagINT;
DECLAREsql1VARCHAR(1000);
DECLAREcursor_nameCURSORFORSELECTTABLE_NAMEFROMinformation_schema.tablesWHERETABLE_NAMELIKE'test_2014%';
'02000'SETstopFlag=1;
SET@sql1='createviewtestasselectid,namefrom';
OPENcursor_name;
REPEAT
FETCHcursor_nameINTOtableName;
SELECTCONCAT(@sql1,tableName,'UNIONALLSELECTid,nameFROM')INTO@sql1FROMDUAL;
UNTILstopFlagENDREPEAT;
CLOSEcursor_name;
SELECTSUBSTR(@sql1,1,LENGTH(@sql1)-31)INTO@sql1FROMDUAL;
PREPAREstepFROM@sql1;
EXECUTEstep;
END;
Ⅳ Mysql里如何使用存儲過程
CREATE DEFINER 存儲過程名
@變數1 類型 ----這里個就是傳進來的
@變數2 類型 output ----這個是定義輸出變數
as
declared @變數 類型,@變數 類型 --這里是定義內部變數 只能這是過程用
if 條件
begin
#¥%……&*
end
else
begin
#¥%……&
end
-----不知道你要什麼
execute 資料庫名.dbo.過程名 -----這個是 在這個過程里 調用別的過程
Ⅳ 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
函數的限制
我們已經很清楚可以在存儲過程中使用的元素了。下面我要講的是前面沒有提到的函數。
Ⅵ 五、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掉了
Ⅶ mysql存儲過程里怎麼循環一張表
給你一個 參考
--存儲過程名和參數,參數中in表示傳入參數,out標示傳出參數,inout表示傳入傳出參數
create procere p_procerecode(in sumdate varchar(10))
begin
declare v_sql varchar(500); --需要執行的SQL語句
declare sym varchar(6);
declare var1 varchar(20);
declare var2 varchar(70);
declare var3 integer;
--定義游標遍歷時,作為判斷是否遍歷完全部記錄的標記
declare no_more_departments integer DEFAULT 0;
--定義游標名字為C_RESULT
DECLARE C_RESULT CURSOR FOR
SELECT barcode,barname,barnum FROM tmp_table;
--聲明當游標遍歷完全部記錄後將標志變數置成某個值
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_departments=1;
set sym=substring(sumdate,1,6); --截取字元串,並將其賦值給一個遍歷
--連接字元串構成完整SQL語句,動態SQL執行後的結果記錄集,在MySQL中無法獲取,因此需要轉變思路將其放置到一個臨時表中(注意代碼中的寫法)。一般寫法如下:
-- 'Create TEMPORARY Table 表名(Select的查詢語句);
set v_sql= concat('Create TEMPORARY Table tmp_table(select aa as aacode,bb as aaname,count(cc) as ccnum from h',sym,' where substring(dd,1,8)=''',sumdate,''' group by aa,bb)');
set @v_sql=v_sql; --注意很重要,將連成成的字元串賦值給一個變數(可以之前沒有定義,但要以@開頭)
prepare stmt from @v_sql; --預處理需要執行的動態SQL,其中stmt是一個變數
EXECUTE stmt; --執行SQL語句
deallocate prepare stmt; --釋放掉預處理段
OPEN C_RESULT; --打開之前定義的游標
REPEAT --循環語句的關鍵詞
FETCH C_RESULT INTO VAR1, VAR2, VAR3; --取出每條記錄並賦值給相關變數,注意順序
--執行查詢語句,並將獲得的值付給一個變數 @oldaacode(注意如果以@開頭的變數可以不用通過declare語句事先聲明)
select @oldaacode:=vcaaCode from T_sum where vcaaCode=var1 and dtDate=sumdate;
if @oldaacode=var1 then --判斷
update T_sum set iNum=var3 where vcaaCode=var1 and dtDate=sumdate;
else
insert into T_sum(vcaaCode,vcaaName,iNum,dtDate) values(var1,var2,var3,sumdate);
end if;
UNTIL no_more_departments END REPEAT; --循環語句結束
CLOSE C_RESULT; --關閉游標
DROP TEMPORARY TABLE tmp_table; --刪除臨時表
end;
Ⅷ mysql 存儲過程
你應該在做統計吧,估計你不會的就是mysql存儲過程的語法 我之前也寫過 很是郁悶 我給你一段代碼 是我用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 order by 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 order by 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 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) 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
Ⅸ MySql 存儲過程遍歷游標出錯
MySql 存儲過程遍歷游標出錯
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET DONE = 1;
這種語句是異常捕獲內容,在mysql中都是使用HANDLER 來捕獲異常的。
建議你看下:MySQL存儲過程詳解
MySQL游標循環遍歷的使用