mysql 觸發器 if then elseif else 的運用
自己第一次寫觸發,想使用兩個三個條件語句並列使用,但是不管怎么寫都保存不了,最后看了吳大哥的博文,試了是if..then ...end if;中使用if並列是可以。
我是不知道是為什么,有大神知道可以詳解。下面貼上我的觸發器:
DROP TRIGGER `down`; CREATE DEFINER=`root`@`localhost` TRIGGER `down` BEFORE UPDATE ON `ne` FOR EACH ROW if '1'='1' then #Step 1 #拆除報警: if new.down_status = '1' then insert into ne_alarm_his (ID, alarm_id, company_id, ne_no , hierarchy , ne_model_id, alarm_type_id , alarm_level, alarm_up_time , alarm_last_time, occur_count, ack_user, ack_time, ack_res, ack_remark, alarmtime, areaid, alermaddress , gps_lon , gps_lat , car_id,AuditStatus ,status) values (uuid(), getSerialNumber('報警編號'),new.company_id, new.ne_no ,new.hierarchy, new.ne_model_id , 1, null, new.down_starttime , now() , null , null , null , null , null , now() , null , new.geographical_address2 , new.gps_lon2 ,new.gps_lat2, new.car_id, '0',null ); end if; #Step 2: #低電量報警 if new.low_power_status = '1' then insert into ne_alarm_his (ID, alarm_id, company_id, ne_no , hierarchy , ne_model_id, alarm_type_id , alarm_level, alarm_up_time , alarm_last_time, occur_count, ack_user, ack_time, ack_res, ack_remark, alarmtime, areaid, alermaddress , gps_lon , gps_lat , car_id,AuditStatus , status) values (uuid(), getSerialNumber('報警編號'), new.company_id, new.ne_no, new.hierarchy, new.ne_model_id , 5, null, new.low_power_starttime , now() , null , null , null , null , null , now() , null , new.geographical_address2 , new.gps_lon2 ,new.gps_lat2, new.car_id, '0', null ); end if; #Step 3: #斷電報警 if new.pwr_down_status = '1' then insert into ne_alarm_his (ID, alarm_id, company_id, ne_no , hierarchy , ne_model_id, alarm_type_id , alarm_level, alarm_up_time , alarm_last_time, occur_count, ack_user, ack_time, ack_res, ack_remark, alarmtime, areaid, alermaddress , gps_lon , gps_lat , car_id, AuditStatus ,status) values (uuid(), getSerialNumber('報警編號'), new.company_id, new.ne_no, new.hierarchy, new.ne_model_id , 6, null, new.pwr_down_starttime , now() , null , null , null , null , null , now() , null , new.geographical_address2 , new.gps_lon2 ,new.gps_lat2, new.car_id, '0', null ); end if; end if;
判斷是否包含字符串,
使用函數 locate('admin','ad')
如果包含,返回>0的數,否則返回0
set @alarmType = new.alerttypeset; if (locate(@alarmType , '9'))>0 and new.run_status !=old.run_status and new.run_status = '1' then ..sql... end if;
mysql中字符串的拼接不能使用加號+,用concat函數:
set @vcommandPara =concat('&Uconfig=','replynumber');
下面是吳大哥的博文:
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);
SELECT gwsbbj.is_alarm into is_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;
CREATE TRIGGER <觸發器名稱> --觸發器必須有名字,最多64個字符,可能后面會附有分隔符.它和MySQL中其他對象的命名方式基本相象.
{ BEFORE | AFTER } --觸發器有執行的時間設置:可以設置為事件發生前或后。
{ INSERT | UPDATE | DELETE } --同樣也能設定觸發的事件:它們可以在執行insert、update或delete的過程中觸發。
ON <表名稱> --觸發器是屬於某一個表的:當在這個表上執行插入、 更新或刪除操作的時候就導致觸發器的激活. 我們不能給同一張表的同一個事件安排兩個觸發器。
FOR EACH ROW --觸發器的執行間隔:FOR EACH ROW子句通知觸發器 每隔一行執行一次動作,而不是對整個表執行一次。
<觸發器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
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
;
|
測試一下
DELETE
FROM
tab1
WHERE
tab1_id=
'0001'
;
|
看看結果
1
2
|
SELECT
*
FROM
tab1;
SELECT
*
FROM
tab2;
|