mysql 觸發器 if then elseif else 的運用
create procedure dbname.proc_getGrade
(stu_no varchar(20),cour_no varchar(10))
BEGIN
declare stu_grade float;
select grade into stu_grade from grade where student_no=stu_no and course_no=cour_no;
if stu_grade>=90 then
select stu_grade,'A';
elseif stu_grade<90 and stu_grade>=80 then
select stu_grade,'B';
elseif stu_grade<80 and stu_grade>=70 then
select stu_grade,'C';
elseif stu_grade70 and stu_grade>=60 then
select stu_grade,'D';
else
select stu_grade,'E';
end if;
END
案列
CREATE TRIGGER cfq
AFTER INSERT ON hc_alarm
FOR EACH ROW
BEGIN
declare is_alarm int;
declare telephoneone VARCHAR(20); //定義
declare telephonetwo VARCHAR(20);
declare telephonethree VARCHAR(20);
declare telephonefour VARCHAR(20);
declare telephonefive VARCHAR(20);
SELECTgwsbbj.is_alarmintois_alarm FROM gwsbbj,(SELECT a.mac from hc_gw a ,hc_std b where b.gwid=a.id and b.id=new.stdid) c //賦值
where new.stdid=gwsbbj.stdid and gwsbbj.gwid=c.mac;
SELECT k.telephoneone, k.telephonetwo,k.telephonethree,k.telephonefour,k.telephonefive into telephoneone,telephonetwo,telephonethree,telephonefour,telephonefive //多個賦值
from hc_gw f ,hc_std g ,hc_install_image k where g.gwid=f.id and g.id=new.stdid and f.mac=k.gwmac;
if NEW.triggertype = 'std' and (NEW.event ='017' or NEW.event ='015') and is_alarm=0 then // 一個if 配一個end if
UPDATE gwsbbj set gwsbbj.is_alarm =1 where gwsbbj.stdid=new.stdid;
if telephoneone is NOT NULL then
insert into bdnr(content,tel) values( CONCAT('安裝在',new.trigger,'發生了',new.title),telephoneone);
END if;
if telephonetwo !='' then
insert into bdnr(content,tel) values( CONCAT('安裝在',new.trigger,'發生了',new.title),telephonetwo);
END if;
if telephonethree !='' then
insert into bdnr(content,tel) values( CONCAT('安裝在',new.trigger,'發生了',new.title),telephonethree);
END if;
if telephonefour !='' then
insert into bdnr(content,tel) values( CONCAT('安裝在',new.trigger,'發生了',new.title),telephonefour);
END if;
if telephonefive !='' then
insert into bdnr(content,tel) values( CONCAT('安裝在',new.trigger,'發生了',new.title),telephonefive);
END if;
elseif
NEW.triggertype = 'std' and (NEW.event ='017' or NEW.event ='015') and is_alarm=1
then
UPDATE gwsbbj set gwsbbj.is_alarm=1 where gwsbbj.stdid=new.stdid;
END if;
END;
if...then{
if...then{}
end if;
if...then{}
end if;
...
}
elseif...then..
end if;
CREATETRIGGER<觸發器名稱> --觸發器必須有名字,最多64個字元,可能後面會附有分隔符.它和MySQL中其他物件的命名方式基本相象.
{BEFORE|AFTER} --觸發器有執行的時間設定:可以設定為事件發生前或後。
{INSERT|UPDATE|DELETE} --同樣也能設定觸發的事件:它們可以在執行insert、update或delete的過程中觸發。
ON<表名稱> --觸發器是屬於某一個表的:當在這個表上執行插入、 更新或刪除操作的時候就導致觸發器的啟用. 我們不能給同一張表的同一個事件安排兩個觸發器。
FOREACH
<觸發器SQL語句> --觸發器包含所要觸發的SQL語句:這裡的語句可以是任何合法的語句, 包括複合語句,但是這裡的語句受的限制和函式的一樣。
--你必須擁有相當大的許可權才能建立觸發器(CREATE TRIGGER),如果你已經是Root使用者,那麼就足夠了。這跟SQL的標準有所不同。
~~例項~~
example1:
建立表tab1
1 2 3 4 |
DROP TABLE IF EXISTS tab1;
CREATE TABLE tab1(
tab1_id varchar (11)
);
|
建立表tab2
1 2 3 4 |
DROP TABLE IF EXISTS tab2;
CREATE TABLE tab2(
tab2_id varchar (11)
);
|
建立觸發器:t_afterinsert_on_tab1
作用:增加tab1表記錄後自動將記錄增加到tab2表中
1 2 3 4 5 6 7 |
DROP TRIGGER IF EXISTS t_afterinsert_on_tab1;
CREATE TRIGGER t_afterinsert_on_tab1
AFTER INSERT ON tab1
FOR EACH ROW
BEGIN
insert into tab2(tab2_id) values (new.tab1_id);
END ;
|
測試一下
1 |
INSERT INTO tab1(tab1_id) values ( '0001' );
|
看看結果
1 2 |
SELECT * FROM tab1;
SELECT * FROM tab2;
|
example2:
建立觸發器:t_afterdelete_on_tab1
作用:刪除tab1表記錄後自動將tab2表中對應的記錄刪去
1 2 3 4 5 6 7 |
DROP TRIGGER IF EXISTS t_afterdelete_on_tab1;
CREATE TRIGGER t_afterdelete_on_tab1
AFTER DELETE ON tab1
FOR EACH ROW
BEGIN
delete from tab2 where tab2_id=old.tab1_id;
END ;
|
測試一下
1 |
DELETE FROM tab1 WHERE tab1_id= '0001' ;
|
看看結果
1 2 |
SELECT * FROM tab1;
SELECT * FROM tab2;
|