可以这么创建
DROP PROCEDURE IF EXISTS text;
CREATE PROCEDURE text()
BEGIN
#这里写过程语句
END;
比如,存储过程我需要查询一个表,可以这么写
DROP PROCEDURE IF EXISTS text;
CREATE PROCEDURE text()
BEGIN
SELECT * from users;
END;
你也可以查询多张表,如
DROP PROCEDURE IF EXISTS text;
CREATE PROCEDURE text()
BEGIN
SELECT * from table1;
SELECT * from table2;
SELECT * from table3;
END;
你不仅可以查询,也能删除,修改
DROP PROCEDURE IF EXISTS text;
CREATE PROCEDURE text()
BEGIN
SELECT * from table1;
SELECT * from table2;
SELECT * from table3;
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
END;
创建好了以后,需要调用才能执行,调用很简单
CALL text();
就像函数一样调用,你也可以定义参数,如
DROP PROCEDURE IF EXISTS text;
CREATE PROCEDURE text(IN name CHAR(50))
BEGIN
SELECT * from tb_appuser WHERE NICK_NAME = name;
END;
参数有三种类型
IN
输入参数:表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值。OUT
输出参数:该值可在存储过程内部被改变,并可返回。INOUT
输入输出参数:调用时指定,并且可被改变和返回。
比如OUT的用法
DROP PROCEDURE IF EXISTS text;
CREATE PROCEDURE text(OUT usd INT)
BEGIN
SELECT usd;
SET usd = 1000;
END;