[置顶] MySQL -- 创建函数(Function
目标
语法
- CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,参数是可选的
- RETURNS type
- [ characteristic ...] routine_body
- CREATE FUNCTION 用来创建函数的关键字;
- func_name 表示函数的名称;
- func_parameters为函数的参数列表,参数列表的形式为:[IN|OUT|INOUT] param_name type
- IN:表示输入参数;
- OUT:表示输出参数;
- INOUT:表示既可以输入也可以输出;
- param_name:表示参数的名称;
- type:表示参数的类型,该类型可以是MySQL数据库中的任意类型;
- RETURNS type:语句表示函数返回数据的类型;
- characteristic: 指定存储函数的特性,取值与存储过程时相同,详细请访问-MySQL存储过程使用;
示例
创建示例数据库、示例表与插入样例数据脚本:
- create database hr;
- use hr;
- create table employees
- (
- employee_id int(11) primary key not null auto_increment,
- employee_name varchar(50) not null,
- employee_sex varchar(10) default '男',
- hire_date datetime not null default current_timestamp,
- employee_mgr int(11),
- employee_salary float default 3000,
- department_id int(11)
- );
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8);
- insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);
- select * from employees;
创建函数-根据ID获取员工姓名与员工工资
- DELIMITER //
- CREATE FUNCTION GetEmployeeInformationByID(id INT)
- RETURNS VARCHAR(300)
- BEGIN
- RETURN(SELECT CONCAT('employee name:',employee_name,'---','salary: ',employee_salary) FROM employees WHERE employee_id=id);
- END//
- DELIMITER ;
调用函数
在mysql——函数的使用方法与MySQL内部函数的使用方法一样。

[置顶] MySQL -- 创建函数(Function的更多相关文章
- MySQL 创建函数(Function)
目标 怎么样MySQL创建数据库功能(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,參数是可选的 RETUR ...
- mysql 创建函数ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_f
mysql 创建函数的时候 报错 ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL D ...
- Mysql创建函数出错
目前在项目中,执行创建mysql的函数出错, mysql 创建函数出错信息如下: Error Code: 1227. Access denied; you need (at least one of) ...
- MySQL创建函数报“ERROR 1418 ”错误,不能创建函数
MySQL创建函数报ERROR 1418错误,不能创建函数,根据官方提示是说,不能创建函数可能是一个安全设置方面的配置或功能未开启原因,下面我们一起来看. 错误 ERROR 1418 (HY000 ...
- MySQL 创建函数失败提示1418
MySQL 创建函数失败提示1418 在创建函数时,往往会遇到创建函数失败的情形,除去书写的创建函数的sql语句本身语法错误之外,还会碰到一个错误就是, 1418:This function has ...
- mysql 创建函数This function has none of DETERMINISTIC, NO SQL, or READS
今天在mysql 5.6上创建函数的时候 发现报错: ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or R ...
- MySQL 创建函数报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators
问题描述 通过Navicat客户端,创建MySQL函数(根据的当前节点查询其左右叶子节点)时报错,报错信息如下: This function has none of DETERMINISTIC, NO ...
- Mysql 创建函数出现This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary mys ...
- 变量声明置顶规则、函数声明及函数表达式和函数的arguments属性初始化
一.变量声明和变量赋值: if (!("a" in window)) { ; } alert(a);//a为? 你可能认为alert出来的结果是1,然后实际结果是“undefine ...
随机推荐
- Watch Before You Feel Pressure
Today's assembly is about the start of a journey. 今天的大会是一个旅程的开始. The start of the rest of your lives ...
- (转)UIPanGestureRecognizer
UIPanGestureRecognizer是UIGestureRecognizer类的一个扩展类,其扩展类有UITapGestureRecognizer,UIPinchGestureRecogniz ...
- 【SaltStack】通过Master给Minion安装MySQL
一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...
- 【10】css hack原理及常用hack
[10]css hack原理及常用hack 原理:利用不同浏览器对CSS的支持和解析结果不一样编写针对特定浏览器样式.常见的hack有1)属性hack.2)选择器hack.3)IE条件注释 IE条件注 ...
- WCF全局异常处理
在用wcf做为单纯的服务端的时候,发生错误是常有的事情,特别是在调用其他系统提供的接口的时候,发生的一些错误总是让人摸不着头脑,严重影响了错误的定位.做.net web开发的时候,我们可以在Globa ...
- JSP标签:jsp内置标签、jstl标签、自定义标签
一.jsp标签的分类: 1)内置标签(动作标签): 不需要在jsp页面导入标签 2)jstl标签: 需要在jsp页面中导入标签 3)自定义标签 : 开发者自行定义,需要在jsp页面导入标签 1 ...
- tensorflow 如何限制显存大小
Python在用GPU跑模型的时候最好开多进程,因为很明显这种任务就是计算密集型的. 用进程池好管理,但是tensorflow默认情况会最大占用显存,尽管该任务并不需要这么多,因此我们可以设置显存的按 ...
- PHP “引号兄弟”
PHP的string最大可以达到2GB,不过很少会用到这么大的字符串. 单引号: 定义一个字符串最简单的方式是使用单引号,而在单引号字符串中要想表达一个单引号,需要在她的前面加个反斜线(\)来进行转义 ...
- Zuma (区间DP)
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- 30分钟学会如何使用Shiro(转自:http://www.cnblogs.com/learnhow/p/5694876.html)
本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...