mysql触发器使用if..then sql elseif then end if; 转自 吴大哥

Posted 邢朝阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql触发器使用if..then sql elseif then end if; 转自 吴大哥相关的知识,希望对你有一定的参考价值。

 

 

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;

以上是关于mysql触发器使用if..then sql elseif then end if; 转自 吴大哥的主要内容,如果未能解决你的问题,请参考以下文章

sql中if then的使用

sql中if then的使用

在 if ... then 中使用插入语句 (SQL)

使用带有变量的 If Then 运行不同的 SQL 语句

oracleif判断语句

如何在 PL/SQL 块 (IF/THEN) 中执行“插入...选择...