MySQL创建存储过程

存储过程和函数的区别参考链接:

http://www.cnblogs.com/lengbingshy/archive/2010/02/25/1673476.html

创建存储过程:

DROP PROCEDURE mypro;
CREATE procedure mypro (sid INT,OUT sCount INT)
BEGIN

SELECT COUNT(*) INTO sCount from student where id = sid;
END

执行该存储过程:

CALL mypro(1,@c);--不能用select来调
select @c;
创建函数:

DROP FUNCTION myfun;
CREATE FUNCTION myfun (sid INT)
RETURNS int
BEGIN
DECLARE sCount int;
SELECT COUNT(*) INTO sCount from student where id=sid;
RETURN sCount;
END

执行函数:
SELECT myfun(1)     --不能用call来调用
JDBC调用存储过程和函数

//
调用存储函数 CallableStatement statement = connection.prepareCall("{?=call myfun(?)}"); //调用存储过程 //CallableStatement statement = connection.prepareCall("{call mypro(?,?)}"); statement.registerOutParameter(1, Types.INTEGER); statement.setInt(2, 1); statement.execute(); int count = statement.getInt(1); System.out.println(count);

总结:jdbc调用存储过程和函数的区别在于sql的区别,存储函数需要显示的写上返回值。但是在代码中统一用call调用,并不需要向在mysql里一样分开用call 和select调用

原文地址:https://www.cnblogs.com/wanjn/p/7499842.html