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

MySQL 自定义函数CREATE FUNCTION示例

程序员文章站 2024-03-02 08:40:10
mysql> mysql> delimiter $$ mysql> mysql> create function myfunction...
mysql>
mysql> delimiter $$
mysql>
mysql> create function myfunction
-> (in_string varchar(255),
-> in_find_str varchar(20),
-> in_repl_str varchar(20))
->
-> returns varchar(255)
-> begin
-> declare l_new_string varchar(255);
-> declare l_find_pos int;
->
-> set l_find_pos=instr(in_string,in_find_str);
->
-> if (l_find_pos>0) then
-> set l_new_string=insert(in_string,l_find_pos,length(in_find_str),in_repl_str);
-> else
-> set l_new_string=in_string;
-> end if;
-> return(l_new_string);
->
-> end$$
query ok, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> select myfunction('abc','a','z');
+---------------------------+
| myfunction('abc','a','z') |
+---------------------------+
| zbc |
+---------------------------+
1 row in set (0.00 sec)

mysql> drop function myfunction;
query ok, 0 rows affected (0.00 sec)