1.概念
存储过程(Stored Procedure)是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象,为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行,思想上很简单,就是数据库 SQL 语言层面的代码封装与重用
--创建一个存储过程的语法为: create procedure 存储过程名(参数列表) begin 存储过程体 end call 存储过程名(参数列表) --如下简单存储过程 DROP PROCEDURE user_procedure; create PROCEDURE user_procedure(in x int) -- in 表示输入 BEGIN select * from `user` where id = x; END -- 执行 call user_procedure(1);
DROP PROCEDURE user_procedure_out; create PROCEDURE user_procedure_out(in x int, out y varchar(10))
BEGIN select user_name into y from `user` where id = x; END -- 执行 call user_procedure_out(1, @a); select @a
2.存储过程理解
show create PROCEDURE user_procedure_out;
3.变量
--语法范例: select 列名列表 into 变量列表 from 表名 --实例 DROP PROCEDURE user_procedure_2; create PROCEDURE user_procedure_2(in x int, out y varchar(10))
BEGIN declare s varchar(10) ; select user_name into s from `user` where id = x; set y = s; END -- 执行 call user_procedure_2(1, @a); select @a
--if 的语法格式为: if 条件表达式 then 语句 [elseif 条件表达式 then 语句] .... [else 语句] end if case 的语法格式 --首先是第一种写法: case 表达式 when 值 then 语句 when 值 then 语句 ... [else 语句] end case --然后是第二种写法: case when 表达式 then 语句 when 表达式 then 语句 .... [else 语句] end case --loop 循环 语法格式为: [标号:] loop 循环语句 end loop [标号] while while a>100 do 循环语句 End while Repeat //游标 SQL语句1 UNTIL 条件表达式 END Repeat; Loop SQL语句 所有的条件判断和跳出需要自己实现 End loop --leave 语句用来从标注的流程构造中退出,它通常和 begin...end 或循环一起使用 leave 标号; --声明语句结束符,可以自定义: DELIMITER [符合] delimiter $$ $$
6.游标
--语法: DECLARE test_cursor CURSOR FOR 结果集; --声明游标 OPEN test_cursor; --打开游标 CLOSE test_cursor; --关闭游标 DECLARE CONTINUE HANDLER FOR NOT FOUND --结果集查询不到数据自动跳出
总结:
--案例 delimiter $$ create procedure exchange(out count int ) begin declare supply_id1 int default 0; declare amount1 int default 0; -- 游标标识 declare blag int default 1; -- 游标 declare order_cursor cursor for select supply_id,amount from order_group; -- not found 这个异常进行处理 declare continue handler for not found set blag = 0; set count = 0; -- 打开游标 open order_cursor; -- 遍历 read_loop: LOOP fetch order_cursor into supply_id1,amount1; if blag = 0 then leave read_loop; end if; if supply_id1 = 1 then set count = count + amount1; end if; end loop read_loop; end; $$ delimiter ; call exchange(@count); select @count;
8.php中的应用
require_once ‘db.php‘; // $sql = ‘create procedure login_procedure_4 (in id int, out ret varchar(10)) // begin // declare y int default 0; // select shop_name into ret from shop where uid = id; // set ret = "ttt"; // if y = 0 then // set ret = "xxxxx"; // end if; // end‘; // $db->execute($sql); // $sql = ‘call login_procedure_3(87, @ret)‘; // var_dump($db->call($sql, ‘select @ret‘)); // echo ‘成功‘; // 游标 // 1. 声明 /* declare xxx declare 游标名 cursor for 查询语句 [select xx,xx,x,x from table_name]; open 游标名 -- 打开 fetch 游标名 into xxx, 业务处理 close 游标名 */ $sql = ‘ create procedure login_procedure_5 (in id int, out ret varchar(10)) begin declare id int default 0; -- 游标标识 declare blag int default 1; declare name varchar(10) default 0; declare shop_cursor cursor for select shop_name,uid from shop; -- 异常处理 :注意游标在读取数据的时候,不断读取 declare continue handler for not found set blag = 0; open shop_cursor; -- 打开 read_loop:loop fetch shop_cursor into name,id; if blag = 0 then leave read_loop; end if; if id = 63 then set ret = name; end if; end loop read_loop; end‘; // $db->execute($sql); $sql1 = ‘call login_procedure_5(87, @ret)‘; var_dump($db->call($sql1, ‘select @ret‘)); echo ‘成功‘;
/** * [call description] * @param string $sql 查询的语句 * @param string $select_param 参数 * @return [type] */ public function call($sql, $select_param = null) { $stmt = $this->pdo->prepare($sql); if ($stmt->execute()) { if (isset($select_param)) { return $this->pdo->query($select_param)->fetchAll(); } else { return $this->pdo->fetchAll(); } return true; } else { return false; } }
9.小结:
原文:https://www.cnblogs.com/XiaKang/p/13198222.html