1.创建语法
CREATE PROCEDURE Pro_name()
BEGIN
...
END
2.定义变量
DELIMITER $$
CREATE PROCEDURE find_name1()
BEGIN
DECLARE uid VARCHAR(50) DEFAULT '';
SET uid='004';
SELECT * FROM student WHERE sid=uid;
SELECT NAME FROM test;
END
CALL find_name1();
3.传入一个参数
DELIMITER $$
CREATE PROCEDURE find_name2(uid VARCHAR(20))
BEGIN
SELECT * FROM student WHERE sid=uid;
END
CALL find_name2('005');
4.可以多个begin—end,也可以返回多个参数
变量作用域说明:
(1)、存储过程中变量是有作用域的,作用范围在begin和end块之间,end结束变量的作用范围即结束。
(2)、需要多个块之间传值,可以使用全局变量,即放在所有代码块之前
(3)、传参变量是全局的,可以在多个块之间起作用
DELIMITER $$
CREATE PROCEDURE find_name5()
BEGIN
BEGIN
DECLARE max_test INT DEFAULT 0;
DECLARE max_student INT DEFAULT 0;
SELECT MAX(age) INTO max_test FROM test;
SELECT MAX(age) INTO max_student FROM student;
SELECT max_test,max_student;
END;
BEGIN
DECLARE count_test INT DEFAULT 0;
DECLARE count_student INT DEFAULT 0;
SELECT COUNT(0) INTO count_test FROM test;
SELECT COUNT(0) INTO count_student FROM student;
SELECT count_test,count_student;
END;
END
CALL find_name5();
5.测试全局变量
DELIMITER $$
CREATE PROCEDURE find_name6()
BEGIN
DECLARE max_test INT DEFAULT 0;
DECLARE max_student INT DEFAULT 0;
BEGIN
SELECT MAX(age) INTO max_test FROM test;
SELECT MAX(age) INTO max_student FROM student;
SELECT max_test;
END;
BEGIN
DECLARE count_test INT DEFAULT 0;
DECLARE count_student INT DEFAULT 0;
SELECT COUNT(0) INTO count_test FROM test;
SELECT COUNT(0) INTO count_student FROM student;
SELECT max_student,count_test,count_student;
END;
END
CALL find_name6
6.in、out参数的用法
in为传入参数,out为输出参数
DELIMITER $$
CREATE PROCEDURE find_name7(IN id VARCHAR(3),OUT myname VARCHAR(100))
BEGIN
SELECT sname INTO myname FROM student WHERE sid=id;
END;
CALL find_name7('001',@myname);
SELECT @myname
7.参数inoutde的使用(既能输入一个值又能传出来一个值)
DELIMITER $$
CREATE PROCEDURE find_name8(INOUT myage INT(20))
BEGIN
SET myage=myage+1;
SELECT age INTO myage FROM student WHERE age=myage;
END
SET @myage=20;
CALL find_name8(@myage);
SELECT @myage
也可以写多个inout参数
DELIMITER $$
CREATE PROCEDURE find_name9(INOUT uid VARCHAR(20),INOUT uname VARCHAR(20))
BEGIN
SET uid='002';
SET uname='';
SELECT sid,sname INTO uid,uname FROM student WHERE sid=uid;
END
CALL find_name9(@uid,@uname);
SELECT @uid,@uname
8.存储过程条件语句用法
①条件语句基本结构 if() then...else...end if;
案例:传入一个年龄,如果年龄为偶数,查询名字,否则查询年龄
DELIMITER $$
CREATE PROCEDURE pro_test1(IN myage INT(20))
BEGIN
DECLARE username VARCHAR(200) DEFAULT '';
IF(myage%2=0)
THEN
SELECT sname INTO username FROM student WHERE age=myage;
SELECT username;
ELSE
SELECT myage;
END IF;
END
CALL pro_test1(20)
CALL pro_test1(21)
②多条件判断语句:
if() then...
elseif() then...
else ...
end if;
案例:传入id,查询性别,如果为男人,年龄+10,女人年龄+5,中性人年龄+2
DELIMITER $$
CREATE PROCEDURE pro_test2(IN id VARCHAR(20))
BEGIN
DECLARE yousex VARCHAR(20) DEFAULT '';
DECLARE youage INT DEFAULT 0;
DECLARE yage INT DEFAULT 0;
SELECT sex ,age INTO yousex,youage FROM student WHERE sid=id;
SELECT age INTO yage FROM student WHERE sid=id;
IF(yousex='男')
THEN
SET youage=youage+10;
ELSEIF(yousex='女')
THEN
SET youage=youage+5;
ELSE
SET youage=youage+2;
END IF;
SELECT yage,yousex,youage;
END
CALL pro_test2('001')
CALL pro_test2('002')
CALL pro_test2('003')
9.存储过程循环语句
①while语句的基本结构
while(表达式) do
......
end while;
案例:循环插入几千条数据
DELIMITER $$ -- 以delimiter来标记用$表示存储过程结束
CREATE PROCEDURE pro_insert1() -- 创建pro_insert()存储方法
BEGIN
DECLARE i INT; -- 定义i变量
SET i=500; -- 对i赋值
WHILE 400 INSERT INTO test (id,NAME,age)VALUES(i,'张三',i);
SET i=i-1; -- 自减循环
END WHILE; -- 结束while循环
END
CALL pro_insert1()