mysql基础--自定义function/procedure
2016-02-24 14:20
816 查看
1 function(自定义返回值 与 系统函数用法相同)
例:
2.procedure
1. IN 表示必须在调用存储过程时指定
2.OUT 可以被存储过程改变并返回
3.INOUT 必须在调用存储过程时指定,可以被存储过程改变并返回
MySql存储过程简单实例:
/********************* 创建表 *****************************/
delimiter //
DROP TABLE if exists test //
CREATE TABLE test(
id int(11) NULL
) //
/********************** 最简单的一个存储过程 **********************/
drop procedure if exists sp//
CREATE PROCEDURE sp() select 1 //
call sp()//
/********************* 带输入参数的存储过程 *******************/
drop procedure if exists sp1 //
create procedure sp1(in p int)
comment 'insert into a int value'
begin
/* 定义一个整形变量 */
declare v1 int;
/* 将输入参数的值赋给变量 */
set v1 = p;
/* 执行插入操作 */
insert into tes
4000
t(id) values(v1);
end
//
/* 调用这个存储过程 */
call sp1(1)//
/* 去数据库查看调用之后的结果 */
select * from test//
/****************** 带输出参数的存储过程 ************************/
drop procedure if exists sp2 //
create procedure sp2(out p int)
/*这里的DETERMINISTIC子句表示输入和输出的值都是确定的,不会再改变.我一同事说目前mysql并没有实现该功能,因此加不加都是NOT DETERMINISTIC的*/
DETERMINISTIC
begin
select max(id) into p from test;
end
//
/* 调用该存储过程,注意:输出参数必须是一个带@符号的变量 */
call sp2(@pv)//
/* 查询刚刚在存储过程中使用到的变量 */
select @pv//
/******************** 带输入和输出参数的存储过程 ***********************/
drop procedure if exists sp3 //
create procedure sp3(in p1 int , out p2 int)
begin
if p1 = 1 then
/* 用@符号加变量名的方式定义一个变量,与declare类似 */
set @v = 10;
else
set @v = 20;
end if;
/* 语句体内可以执行多条sql,但必须以分号分隔 */
insert into test(id) values(@v);
select max(id) into p2 from test;
end
//
/* 调用该存储过程,注意:输入参数是一个值,而输出参数则必须是一个带@符号的变量 */
call sp3(1,@ret)//
select @ret//
/***************** 既做输入又做输出参数的存储过程 ***************************************/
drop procedure if exists sp4 //
create procedure sp4(inout p4 int)
begin
if p4 = 4 then
set @pg = 400;
else
set @pg = 500;
end if;
select @pg;
end//
call sp4(@pp)//
/* 这里需要先设置一个已赋值的变量,然后再作为参数传入 */
set @pp = 4//
call sp4(@pp)//
/********************************************************/
例:
2.procedure
1. IN 表示必须在调用存储过程时指定
2.OUT 可以被存储过程改变并返回
3.INOUT 必须在调用存储过程时指定,可以被存储过程改变并返回
MySql存储过程简单实例:
/********************* 创建表 *****************************/
delimiter //
DROP TABLE if exists test //
CREATE TABLE test(
id int(11) NULL
) //
/********************** 最简单的一个存储过程 **********************/
drop procedure if exists sp//
CREATE PROCEDURE sp() select 1 //
call sp()//
/********************* 带输入参数的存储过程 *******************/
drop procedure if exists sp1 //
create procedure sp1(in p int)
comment 'insert into a int value'
begin
/* 定义一个整形变量 */
declare v1 int;
/* 将输入参数的值赋给变量 */
set v1 = p;
/* 执行插入操作 */
insert into tes
4000
t(id) values(v1);
end
//
/* 调用这个存储过程 */
call sp1(1)//
/* 去数据库查看调用之后的结果 */
select * from test//
/****************** 带输出参数的存储过程 ************************/
drop procedure if exists sp2 //
create procedure sp2(out p int)
/*这里的DETERMINISTIC子句表示输入和输出的值都是确定的,不会再改变.我一同事说目前mysql并没有实现该功能,因此加不加都是NOT DETERMINISTIC的*/
DETERMINISTIC
begin
select max(id) into p from test;
end
//
/* 调用该存储过程,注意:输出参数必须是一个带@符号的变量 */
call sp2(@pv)//
/* 查询刚刚在存储过程中使用到的变量 */
select @pv//
/******************** 带输入和输出参数的存储过程 ***********************/
drop procedure if exists sp3 //
create procedure sp3(in p1 int , out p2 int)
begin
if p1 = 1 then
/* 用@符号加变量名的方式定义一个变量,与declare类似 */
set @v = 10;
else
set @v = 20;
end if;
/* 语句体内可以执行多条sql,但必须以分号分隔 */
insert into test(id) values(@v);
select max(id) into p2 from test;
end
//
/* 调用该存储过程,注意:输入参数是一个值,而输出参数则必须是一个带@符号的变量 */
call sp3(1,@ret)//
select @ret//
/***************** 既做输入又做输出参数的存储过程 ***************************************/
drop procedure if exists sp4 //
create procedure sp4(inout p4 int)
begin
if p4 = 4 then
set @pg = 400;
else
set @pg = 500;
end if;
select @pg;
end//
call sp4(@pp)//
/* 这里需要先设置一个已赋值的变量,然后再作为参数传入 */
set @pp = 4//
call sp4(@pp)//
/********************************************************/
相关文章推荐
- mysql 常用函数
- MySQL Varchar的新认识
- MySQL中同时存在创建和上次更新时间戳字段解决方法浅析
- Can’t connect to local MySQL server through socket
- mysql 忽略主键冲突、避免重复插入的几种方式
- mysql的安装与mysql一机多实例(低版本)
- mysql全局权限账户%登录不上ERROR 1045 (28000): Access denied for user 'mhz'@'localhost' (using password: YES)
- Mysql + keepalived 实现双主热备读写分离
- mysql数据库编码设置
- MySQL查询对NULL的处理
- mysql cluster搭建
- mysql表分区探索
- mysql自动断开连接的问题处理
- mysql远程连接数据库很慢
- MySQL 清除表空间碎片
- MySql主主(主从)同步配置详解
- Mysql AB 复制
- 解决mysql使用GTID主从复制错误问题
- MySQL主从复制结构中常用参数
- mysql主从同步(低版本)参数说明