1、简单存储过程的创建和调用
//存储过程test1
create PROCEDURE test1()
begin//开始
select * from cst_customer;//中间要做的操作
end//结束
//存储过程的调用
Call test1()
2、创建带参数的存储过程
create PROCEDURE test2(IN c_id BIGINT(20))
begin
select * from cst_customer where cust_id = c_id;
end
call test2(2)
3、创建输出参数的存储过程
create PROCEDURE test3(OUT c_name VARCHAR(32))
BEGIN
set c_name = '6666';
SELECT c_name;
END
call test3(@c_name)//6666
4、变量
CREATE PROCEDURE test4()
BEGIN
DECLARE id BIGINT(32) DEFAULT 2;
SELECT * FROM cst_customer where cust_id = id;
END
call test4()
使用存储过程循环插入sql语句
create PROCEDURE test()
begin
DECLARE i bigint;
set i=2;
while i<=20 do
INSERT INTO `audit_record_risk`(`ID`….) VALUES (i,….);
set i=i+1;
END while;
END;
Call test()
总结:
- 如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;
- 如果仅仅从MySQL存储过程返回值,那就用out类型参数;
- 如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。
查看存储过程;SHOW CREATE PROCEDURE proc_name;
查看函数:SHOW CREATE FUNCTION func_name;
删除存储过程:DROP PROCEDURE proc_name;
删除函数:DROP FUNCTION func_nmae