– 创建存储过程
CREATE 存储过程的名称(参数)
BEGIN
…需要执行的SQL语句
END
– 调用
CALL 存储过程的名称(参数)
个人看法,这就是一个函数…
BEGIN
SELECT * FROM student;
END
CALL p_student_select()
CREATE PROCEDURE p_count(OUT count int)
BEGIN
SELECT COUNT(*) into count FROM student;
END
set @num=10; # 定义变量
CALL p_count(@num); #我的数据有11条
SELECT @num as num; #这里的num已经是11了
–这个就是一个调用参数的函数
CREATE PROCEDURE p_studnt_selectById(in id int)
BEGIN
SELECT * FROM student WHERE studentNo=id;
END
CALL p_studnt_selectById(4)
CREATE PROCEDURE p_result_test(out num int, in id int)
BEGIN
SELECT COUNT(*) INTO num FROM result WHERE studentNo=id;
END
set @num=0;
CALL p_result_test(@num, 3);
SELECT @num as num
CREATE PROCEDURE p5()
BEGIN
DECLARE studentName VARCHAR(10); #创建了一个变量
set @studentName=’HELLO’; #给变量赋值
END
CALL p5();
SELECT @studentName
— 说明了declare定义的变量可以在外部直接调用
CREATE PROCEDURE p6(in id int)
BEGIN
IF(id=0) THEN
SELECT * FROM student;
ELSE
SELECT * FROM teacher;
END IF;
END
CREATE PROCEDURE p7(in tab VARCHAR(10))
BEGIN
CASE tab
WHEN ‘student’ THEN
SELECT * FROM student;
WHEN ‘teacher’ THEN
SELECT * FROM teacher;
END CASE;
END
BEGIN
addloop: LOOP
set id=id + 1;
SELECT id;
IF id > 10 THEN
LEAVE addloop;
ELSE
SELECT * FROM student;
END IF;
END LOOP addloop;
END
BEGIN
REPEAT
set count=count + 1;
SELECT count;
UNTIL count > 10 END REPEAT; #count >10跳出
END
BEGIN
while num < 10 DO
SELECT num;
SET num=num + 1;
END WHILE;
END
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。