当前位置:Gxlcms > 数据库问题 > mysql之存储过程

mysql之存储过程

时间:2021-07-01 10:21:17 帮助过:4人阅读

1. 用于替代程序写的SQL语句,实现程序与sql解耦 #2. 基于网络传输,传别名的数据量小,而直接传sql数据量大

无参的存储过程

delimiter //
create procedure p1()
BEGIN
    select * from blog;
    INSERT into blog(name,sub_time) values("xxx",now());
END //
delimiter ;

#在mysql中调用
call p1() 

#在python中基于pymysql调用
cursor.callproc(p1) 
print(cursor.fetchall())

有参的存储过程

对于存储过程,可以接收参数,其参数有三类:

#in          仅用于传入参数用
#out        仅用于返回值用
#inout     既可以传入又可以当作返回值

带in的存储过程

mysql> select * from emp;
+----+----------+-----+--------+
| id | name     | age | dep_id |
+----+----------+-----+--------+
|  1 | zhangsan |  18 |      1 |
|  2 | lisi     |  19 |      1 |
|  3 | egon     |  20 |      2 |
|  5 | alex     |  18 |      2 |
+----+----------+-----+--------+
4 rows in set (0.30 sec)

mysql> delimiter //
mysql> create procedure p2(in n1 int, in n2 int)
    ->  begin
    ->   select * from emp where id >n1 and id <n2;
    ->  end  //
Query OK, 0 rows affected (0.28 sec)

mysql> delimiter ;
mysql> call p2(1,3)
    -> ;
+----+------+-----+--------+
| id | name | age | dep_id |
+----+------+-----+--------+
|  2 | lisi |  19 |      1 |
+----+------+-----+--------+
1 row in set (0.07 sec)

Query OK, 0 rows affected (0.07 sec)
#在python中基于pymysql调用
cursor.callproc(p2,(1,3))
print(cursor.fetchall())

 

mysql之存储过程

标签:rom   etc   set   参数   print   通过   sel   hang   ted   

人气教程排行