MySQL custom function writing

Requirement: For records in the database, if there are two consecutive data with the same value of the value field, send an email to notify the relevant personnel.

 

Requirement analysis: The alarm check in the alarm system is carried out through SQL. General SQL cannot realize this function. Therefore, it can be realized by function here.

 

The specific implementation is as follows:

1. Data table preparation: tb_config_record. The new data table script is as follows:

 

   create table tb_config_record (id int ,status  varchar(20), value  varchar(20), updated_at datetime, updated_by varchar(20), created_at datetime, created_by varchar(20)) ;

 

2. Data preparation. The script is as follows:

  

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(1,'SUCCESS','0',now(),'SYS',now(),'SYS');

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(2,'SUCCESS','1',now(),'SYS',now(),'SYS');

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(3,'SUCCESS','2',now(),'SYS',now(),'SYS');

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(4,'SUCCESS','3',now(),'SYS',now(),'SYS');

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(5,'SUCCESS','4',now(),'SYS',now(),'SYS');

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(6,'SUCCESS','6',now(),'SYS',now(),'SYS');

 

insert into tb_config_record (id , status ,value ,updated_at ,updated_by , created_at , created_by) values(7,'SUCCESS','6',now(),'SYS',now(),'SYS');

 

 

3, function preparation.

    

 

delimiter //

create function has_serial_status() returns integer

begin

declare val varchar(20) ;

declare tmp varchar(20)  default null ;

declare result integer default 0 ;

declare  value_serials_cursor  cursor for select value from tb_config_record where status = 'SUCCESS' order by id asc ;

declare exit handler for not found  return result;

open value_serials_cursor ;

loop_serials : loop 

fetch value_serials_cursor into val ;

if val = tmp then

set result = 1 ;

leave loop_serials ;

else

set tmp = val ;

end if;

end loop ;

close value_serials_cursor ;

return result ;

end //

 

delimiter ;

 

4. Execution results

 

 

After deleting the record with id=7, re-execute the function



 

Summarize:

   The function behaves as expected and gets the correct result. The following difficulties were encountered during the specific operation:

 1. When writing a function, each line must be written at the top, otherwise the following error will occur

     


 Running error:


 

2、需要在函数语句中添加语句:declare exit handler for not found  return result;

    否则出现如下错误:

 
 

 
 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522480&siteId=291194637