MySQL的存储过程
一、创建存储过程
二、变量
三、流程控制
四、
DROP PROCEDURE IF EXISTS sp_test;
CREATE PROCEDURE sp_test(
IN p1 BIGINT,
IN p2 CHAR(64),
IN p3 CHAR(16)
)
BEGIN
--do somethings
END;
CREATE PROCEDURE sp_test(
IN p1 BIGINT,
IN p2 CHAR(64),
IN p3 CHAR(16)
)
BEGIN
--do somethings
END;
二、变量
--定义变量
DECLARE _id INT DEFAULT 0;
--为变量赋值(注:此方法将自动转换数据类型)
SELECT `id` INTO _id FROM `table` WHERE `id` = 1;
--为变量赋值(注:此方法不会自动转换数据类型)
SET _id = 1;
SELECT _id = 1;
DECLARE _id INT DEFAULT 0;
--为变量赋值(注:此方法将自动转换数据类型)
SELECT `id` INTO _id FROM `table` WHERE `id` = 1;
--为变量赋值(注:此方法不会自动转换数据类型)
SET _id = 1;
SELECT _id = 1;
三、流程控制
--IFELSE
IF 1 = 1 THEN
--sql 1;
ELSE
--sql 2
END IF;
--WHILE
DROP PROCEDURE IF EXISTS test1;
CREATE PROCEDURE test1()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 100 DO
insert into rank_curr (rank_id,mbr_id,rank_val,rank_num)
values
(1, 10000+i, 1000-i, i);
SET i = i+ 1;
END WHILE;
END;
CALL test1;
IF 1 = 1 THEN
--sql 1;
ELSE
--sql 2
END IF;
--WHILE
DROP PROCEDURE IF EXISTS test1;
CREATE PROCEDURE test1()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 100 DO
insert into rank_curr (rank_id,mbr_id,rank_val,rank_num)
values
(1, 10000+i, 1000-i, i);
SET i = i+ 1;
END WHILE;
END;
CALL test1;
四、