欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql 创建function

程序员文章站 2022-03-08 08:17:43
...

drop FUNCTION  IF EXISTS WORKNO;
create function WORKNO(projectno VARCHAR(5),workno VARCHAR(10) )
returns VARCHAR(100)

begin
declare seq integer;
declare timestr integer;
declare temp_seq integer;
declare number_return VARCHAR(100);

set timestr = DATE_FORMAT(now(),'%Y');

select sequence+1 INTO temp_seq from pe_number where  work_no = workno AND project_no=projectno;
UPDATE pe_number SET sequence = temp_seq WHERE work_no = workno AND project_no=projectno;

IF ROW_COUNT() = 0 THEN
SET seq = 1;
INSERT INTO pe_number(project_no,work_no,timestr,sequence) values(projectno,workno,timestr,seq);
end IF;

select CONCAT(project_no,work_no,timestr,LPAD(sequence,6,0)) INTO number_return from pe_number WHERE work_no = workno AND project_no=projectno;

return number_return;
end;