准备工作:导入company数据库

1、创建存储过程和函数

1.1创建存储过程

创建名为proc_test()的存储过程,delimiter为分隔符。

mysql> delimiter $$

mysql> create procedure proc_test()

    -> begin

    -> select sal from t_employee;

    -> end

    -> $$

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ; #注意空格

mysql> call proc_test(); #调用存储过程

 

创建一个获取表t_employee记录条数的存储过程,名称为proc_countcount(*)计算后把结果放入参数param1中。

mysql> delimiter $$

mysql> create procedure proc_count(out param1 int)

begin

select count(*) into param1 from t_employee;

end

$$

Query OK, 0 rows affected (0.00 sec)

> call proc_count(@param1); #调用存储过程

> select @param1; #查看结果

 

创建一个获取表t_employee记录条数的存储过程,名称为proc_countcount(*)计算后把结果放入参数param1

mysql> delimiter $$

mysql> create procedure proc_count3(in sal_value double(10,2), out num int)

begin

select count(*) into num from t_employee where sal>sal_value;

end

$$

Query OK, 0 rows affected (0.00 sec)

 

> call proc_count3(1000, @num); #调用存储过程

> select @num; #查看结果

 

1.2创建函数

创建名为func_test的函数

mysql> delimiter $$

mysql> create function func_test(empno int(11))

    -> returns double(10,2)

    -> begin

    -> return (select sal from t_employee where t_employee.empno=empno);

    -> end

    -> $$

Query OK, 0 rows affected (0.00 sec)

 

mysql> delimiter ;

mysql> select func_test(7369); #调用函数

 

 

2. 关于存储过程和函数的表达式

 

2.1 操作变量

1、声明变量

MySQL中定义变量通过关键字DECLARE来实现,其语法形式如下:

DECLARE var_name[,…] type [DEFAULT value]

2、赋值变量

MySQL中为变量赋值通过关键字SET来实现,其语法形式如下:

SET var_name=expr[,…]

当为变量赋值时,除了上述语法外,还可以通过关键字“SELECT……INTO”语句来实现,其语法形式如下:

SELECT field_name[,…] INTO var_name[,…]

      FROM table_name

           WHERE condition

 

3、定义条件和处理程序

> create table test.t(s1 int, primary key(s1));

>delimiter $$

>create procedure handlerdemo()

Begin

Declare continue handler for SQLSTATE ‘23000’ set @x2=1;

Set @x=1;

Insert into test.t values (1);

Set @x=2;

Insert into test.t values (1);

Set @x=3;

Insert into test.t values (1);

End

 

2.2 使用游标(游标只能在存储过程和函数中使用)

查询语句可能返回多条记录,如果数据量非常大,则需要在存储过程和存储函数中使用光标来组条读取查询结果集中的记录。应用程序可以根据需要滚动或浏览其中的数据。

1、声明游标

DECLARE cursor_name CURSOR FOR select_statement;

2、打开游标

OPEN cursor_name

3、使用游标

FETCH cursor_name INTO var_name [,var_name] …

4、关闭游标

CLOSE cursor_name

创建名为proc_test()的存储过程,delimiter为分隔符。

>drop procedure employee_count;

>delimiter $$

创建存储过程

>create procedure employee_count(OUT NUM integer)

BEGIN

声明变量

declare employee_sal integer;

declare flag integer;

声明游标

declare cursor_employee

cursor for select sal from t_employee;

设置结束标志

set flag=1;

set num=0;

打开游标

open cursor_employee;

fetch cursor_employee into employee_sal;

while flag<>1 do

if employee_sal  >600  then

set num=num+1;

end if;

遍历游标指向的结果集

fetch cursor_employee into employee_sal;

end while;

关闭游标

close cursor_employee;

END

$$

> delimiter ;

 

3、查看存储过程和函数

 

3.1 通过SHOW PROCEDURE STATUS语句查看储存过程状态信息

> show procedure status like 'employee%' \G

 

3.2通过SHOW FUNCTION STATUS语句查看函数状态信息

> show function status like  'func_%' \G

 

3.3通过SHOW CREATE PROCEDURE语句查看储存过程定义信息

> show create procedure proc_test \G;

 

3.4通过SHOW CREATE FUNCTION语句查看函数定义信息

> show create function func_test \G

 

3.5 information_schema.Routines表中查看存储过程和函数的信息

> select * from information_schema.Routines

> where routine_name='proc_test' and routine_type='procedure'\G

 

> select * from information_schema.Routines

> where routine_name='func_test' and routine_type='function'\G