當前位置:首頁 » 存儲配置 » mysql存儲過程ifnot

mysql存儲過程ifnot

發布時間: 2023-06-26 12:34:57

❶ mysql存儲過程中先判斷資料庫中是否存在table1表,有就刪除,沒有就新建

是的 這個主要是在增刪改查的時候用到
if TABLE1 EXISTING DROP TABLE1;
CREATE TABLE

❷ 五、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存儲過程的if判斷有多個條件該怎麼優化效率

這個應該不會太慢吧,我建議你看一下,你是不是循環做了太多次的插入/更新操作。
mysql默認的配置中,每次事務提交都要寫binlog和redo log,如果循環太多次——比如循環插入10w條記錄——就會非常慢。一般優化思路分兩種:
1 修改 sync_binlog為一個100-1000間的值,讓binlog每隔100-1000個事務後再寫一次;修改innodb_flush_log_at_trx_commit =2; 這么搞的好處是降低了寫log的次數和消耗的時間,缺點是,中間出錯的話,會丟失一部分的binlog和redolog導致無法通過他們來在出問題是恢復生產庫數據。
2 將所有的插入/更新操作放到一個事務中進行。這樣,顯然就只需要一次寫binlong和redolog咯。

❹ Mysql 存儲過程中如何判斷Cursor中結果集是否為空

0 通過定義一個上下文管理者(即declare continue handler)來實現
必須在游標定義後定義,並通過使用一個輔助變數來進行判斷。

1 示例如下:

delimiter $
drop procere if exists curdemo $
CREATE PROCEDURE curdemo(pid int)
BEGIN
DECLARE notfound INT DEFAULT 0; #定義一個輔助變數用於判斷
DECLARE a int; #定義游標輸出值賦予的變數
DECLARE cur1 CURSOR FOR SELECT id FROM test.t where id= pid; #定義游標
DECLARE CONTINUE HANDLER FOR NOT FOUND SET notfound = 1; #定義declare continue handler,這個會根據上下文是否有結果判斷是否執行SET notfound = 1

OPEN cur1;
FETCH cur1 INTO a;
if notfound = 1 then
select 'no result';
#寫業務邏輯
ELSE
select concat('result:', a);
#寫業務邏輯
end if;
CLOSE cur1;
END
$
delimiter ;

call curdemo(240);

❺ 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 存儲過程總結(二)if語句、參數

1、if :用於做條件判斷,具體的語法結構為:

在if條件判斷的結構中,ELSE IF 結構可以有多個,也可以沒有。 ELSE結構可以有,也可以沒有。

案列:

根據定義的分數score變數,判定當前分數對應的分數等級。

score >= 90分,等級為優秀。

score >= 80分,等級為良好

score >= 60分,等級為及格

score < 60分,等級為不及格。

上述的需求我們雖然已經實現了,但是也存在一些問題,比如:score 分數我們是在存儲過程中定義 死的,而且最終計算出來的分數等級,我們也僅僅是最終查詢展示出來而已。

那麼我們能不能,把score分數動態的傳遞進來,計算出來的分數等級是否可以作為返回值返回呢? 答案是肯定的,我們可以通過接下來所講解的 參數 來解決上述的問題。

2、參數的類型

主要分為以下三種:IN、OUT、INOUT。 具體的含義如下:

(1)in :該類參數作為輸入,也就是需要調用時傳入值 默認

(2)out:該類參數作為輸出,也就是該參數可以作為返回值

(3)inout:既可以作為輸入參數,也可以作為輸出參數

用法:

案例一:

案列二:

❼ mysql存儲過程中,loop……end loop後用select 列明,無法顯示數據,結果顯示no data to fetch,為什麼

要做異常捕捉
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

實例如下:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1;
OPEN cur2;

REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;

CLOSE cur1;
CLOSE cur2;
END

❽ navicat 8.0 mysql中如何調用存儲過程

CREATE DEFINER=`root`@`localhost` PROCEDURE `curdemo`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE userid,repid INT;
DECLARE cur1 CURSOR FOR select u.userid,u.repid from user u,personalwebsite p where
p.url u.repid and p.distributorid = u.userid
and p.url = u.userid;
DECLARE CONTINUE HANDLER FOR SQLSTATE \'02000\' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO userid,repid;
IF NOT done THEN
update personalwebsite set url = repid where distributorid = userid;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
END;

熱點內容
快速指數演算法 發布:2025-02-04 20:20:40 瀏覽:297
python在類中定義函數調用函數 發布:2025-02-04 20:14:47 瀏覽:594
安卓手機的壁紙是哪個 發布:2025-02-04 20:14:44 瀏覽:198
java發展前景 發布:2025-02-04 20:10:19 瀏覽:76
mac登陸密碼哪裡設置 發布:2025-02-04 19:50:20 瀏覽:525
手游腳本封號 發布:2025-02-04 19:42:12 瀏覽:435
玩單機游戲要哪些配置的電腦 發布:2025-02-04 19:17:41 瀏覽:1003
c語言編程圖書 發布:2025-02-04 19:01:52 瀏覽:898
在哪裡開啟密碼顯示 發布:2025-02-04 18:38:30 瀏覽:791
怎麼查詢qq密碼 發布:2025-02-04 18:20:10 瀏覽:515