mysql 的存储过程

创建存储过程之前我们必须修改mysql语句默认结束符 ; 要不然我们无法创建成功

使用delimiter可以修改执行符号DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错,所以要事先用DELIMITER关键字声明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。

修改操作

mysql> delimiter %;

 创建

mysql> create procedure zrd () 
    -> begin 
    -> select * from emp;
    -> end%
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

  调用

mysql> call zrd;
+-------+------------+-----------+------+---------------------+---------+---------+--------+
| empmo | ename      | job       | mgr  | hiredate            | sal     | comm    | deptno |
+-------+------------+-----------+------+---------------------+---------+---------+--------+
|  7369 | SMITH      | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
|  7499 | ALLEN      | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
|  7521 | WARD       | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
|  7566 | JONES      | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN     | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE      | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
|  7782 | CLARK      | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
|  7788 | SCOTT      | ANALYST   | 7566 | 1982-12-09 00:00:00 | 3000.00 |    NULL |     20 |
|  7839 | KING       | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
|  7844 | TURNER     | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS      | CLERK     | 7788 | 1983-01-12 00:00:00 | 1100.00 |    NULL |     20 |
|  7900 | JAMES      | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
|  7902 | FORD       | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
|  7934 | MILLER     | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
|  7777 |   zhang    | NULL      | NULL | NULL                | 2356.54 |  234.45 |   NULL |
+-------+------------+-----------+------+---------------------+---------+---------+--------+
15 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

  查看所有存储过程

mysql> show procedure status \G

  查看存储过程函数

mysql> show create procedure zrd \G
*************************** 1. row ***************************
           Procedure: zrd
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `zrd`()
begin 
select * from emp;
end
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)

  删除存储过程

mysql> drop procedure zrd;
Query OK, 0 rows affected (0.33 sec)

  

posted @ 2020-04-12 13:28  烟雨楼台,行云流水  阅读(208)  评论(0编辑  收藏  举报