一、存储过程的用法
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(in 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 、loop、repeat
①while语句的基本结构
- while(表达式) do
- ......
- end while;
案例:循环插入几千条数据
DELIMITER $$ -- 以delimiter来标记用$表示存储过程结束
CREATE PROCEDURE pro_insert1() -- 创建pro_insert()存储方法
BEGIN
DECLARE i INT; -- 定义i变量
SET i=500; -- 对i赋值
WHILE 400<i DO -- 设置i的循环条件
INSERT INTO test (id,NAME,age)VALUES(i,'张三',i);
SET i=i-1; -- 自减循环
END WHILE; -- 结束while循环
END
CALL pro_insert1()
②loop循环
loop 循环语法:
loop_name:loop
if 条件 THEN -- 满足条件时离开循环
leave loop_name; -- 和 break 差不多都是结束语句
end if;
end loop;
案例:输入一个参数,求从1-该参数的和
DELIMITER $$
CREATE PROCEDURE getsum(IN num INT)
BEGIN
DECLARE sums INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
loop_name:LOOP
IF i<=num
THEN
SET sums=sums+i;
SET i=i+1;
ELSE
LEAVE loop_name;
END IF;
END LOOP;
SELECT sums;
END
CALL getsum(5);
案例2:
delimiter $$
create procedure sum2(in a int)
begin
declare getsum int default 0;
declare i int default 1;
loop_name:loop
if i>a then
leave loop_name;
end if;
set getsum=getsum+i;
set i=i+1;
end loop;
select getsum;
end
CALL sum2(5);
③repeat循环
语法
repeat
循环体
until 条件 end repeat;
案例:
DELIMITER $$
CREATE PROCEDURE sum1(IN num INT)
BEGIN
DECLARE sums INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
REPEAT
SET sums=sums+i;
SET i=i+1;
UNTIL i>num END REPEAT;
SELECT sums;
END
CALL sum1(5)
10MySQL存储过程的基本函数
(1).字符串类
CHARSET(str) //返回字串字符集
CONCAT (string2 [,... ]) //连接字串
INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0
LCASE (string2 ) //转换成小写
LEFT (string2 ,length ) //从string2中的左边起取length个字符
LENGTH (string ) //string长度
LOAD_FILE (file_name ) //从文件读取内容
LOCATE (substring , string [,start_position ] ) 同INSTR,但可指定开始位置
LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length
LTRIM (string2 ) //去除前端空格
REPEAT (string2 ,count ) //重复count次
REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str
RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length
RTRIM (string2 ) //去除后端空格
STRCMP (string1 ,string2 ) //逐字符比较两字串大小,
SUBSTRING (str , position [,length ]) //从str的position开始,取length个字符,
注:mysql中处理字符串时,默认第一个字符下标为1,即参数position必须大于等于1
TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符
UCASE (string2 ) //转换成大写
RIGHT(string2,length) //取string2最后length个字符
SPACE(count) //生成count个空格
(2).数学类
ABS (number2 ) //绝对值
BIN (decimal_number ) //十进制转二进制
CEILING (number2 ) //向上取整
CONV(number2,from_base,to_base) //进制转换
FLOOR (number2 ) //向下取整
FORMAT (number,decimal_places ) //保留小数位数
HEX (DecimalNumber ) //转十六进制
注:HEX()中可传入字符串,则返回其ASC-11码,如HEX('DEF')返回4142143
也可以传入十进制整数,返回其十六进制编码,如HEX(25)返回19
LEAST (number , number2 [,..]) //求最小值
MOD (numerator ,denominator ) //求余
POWER (number ,power ) //求指数
RAND([seed]) //随机数
ROUND (number [,decimals ]) //四舍五入,decimals为小数位数]
注:返回类型并非均为整数
(3).日期时间类
ADDTIME (date2 ,time_interval ) //将time_interval加到date2
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE ( ) //当前日期
CURRENT_TIME ( ) //当前时间
CURRENT_TIMESTAMP ( ) //当前时间戳
DATE (datetime ) //返回datetime的日期部分
DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间
DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime
DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间
DATEDIFF (date1 ,date2 ) //两个日期差
DAY (date ) //返回日期的天
DAYNAME (date ) //英文星期
DAYOFWEEK (date ) //星期(1-7) ,1为星期天
DAYOFYEAR (date ) //一年中的第几天
EXTRACT (interval_name FROM date ) //从date中提取日期的指定部分
MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串
MAKETIME (hour ,minute ,second ) //生成时间串
MONTHNAME (date ) //英文月份名
NOW ( ) //当前时间
SEC_TO_TIME (seconds ) //秒数转成时间
STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示
TIMEDIFF (datetime1 ,datetime2 ) //两个时间差
TIME_TO_SEC (time ) //时间转秒数]
WEEK (date_time [,start_of_week ]) //第几周
YEAR (datetime ) //年份
DAYOFMONTH(datetime) //月的第几天
HOUR(datetime) //小时
LAST_DAY(date) //date的月的最后日期
MICROSECOND(datetime) //微秒
MONTH(datetime) //月
MINUTE(datetime) //分返回符号,正负或0
SQRT(number2) //开平方