MySQL存储过程
-- 建立一张表
CREATE TABLE matches
(
MATCHNO
int(11) NOT NULL,
TEAMNO
int(11) DEFAULT NULL,
PLAYERNO
int(11) DEFAULT NULL,
WON
int(255) DEFAULT NULL,
LOST
int(255) DEFAULT NULL,
PRIMARY KEY (MATCHNO
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入5条数据
INSERT INTO mytest
.matches
(MATCHNO
, TEAMNO
, PLAYERNO
, WON
, LOST
) VALUES (1, 1, 6, 3, 1);
INSERT INTO mytest
.matches
(MATCHNO
, TEAMNO
, PLAYERNO
, WON
, LOST
) VALUES (7, 1, 57, 3, 0);
INSERT INTO mytest
.matches
(MATCHNO
, TEAMNO
, PLAYERNO
, WON
, LOST
) VALUES (8, 1, 8, 0, 3);
INSERT INTO mytest
.matches
(MATCHNO
, TEAMNO
, PLAYERNO
, WON
, LOST
) VALUES (9, 2, 27, 3, 2);
INSERT INTO mytest
.matches
(MATCHNO
, TEAMNO
, PLAYERNO
, WON
, LOST
) VALUES (11, 2, 112, 2, 3);
SELECT * FROM matches; -- 5条数据
-- 创建存储过程
CREATE PROCEDURE delete_matches(IN p_playerno INTEGER) -- IN 代表输入参数,INTEGER整型
BEGIN
DELETE FROM matches
WHERE playerno = p_playerno;
END
-- 调用存储过程
CALL delete_matches(57);
SELECT * FROM matches; -- 4条数据,已删除57号运动员的数据
二、存储过程的参数
存储过程可以有0个或多个参数,用于存储过程的定义。
3种参数类型:
IN输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)
OUT输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
INOUT输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)