一.创建存储过程
1.基本语法:
create procedure sp_name()
begin
………
end
2.参数传递
二.调用存储过程
1.基本语法:call sp_name()
注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递
注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递
三.删除存储过程
1.基本语法:
drop procedure sp_name//
2.注意事项
(1)不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程
drop procedure sp_name//
2.注意事项
(1)不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程
四.区块,条件,循环
1.区块定义,常用
begin
……
end;
也可以给区块起别名,如:
lable:begin
………..
end lable;
可以用leave lable;跳出区块,执行区块以后的代码
2.条件语句
if
<wbr>条件<wbr></wbr></wbr>
then
statement
else
statement
end
<wbr></wbr>
if
;
[
label:
]
<wbr></wbr>
WHILE
<wbr>expression_r<wbr>DO<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top">statements<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"></wbr></wbr>
END
<wbr></wbr>
WHILE
<wbr></wbr>
[
label
]
<wbr>;<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"></wbr>
begin
……
end;
也可以给区块起别名,如:
lable:begin
………..
end lable;
可以用leave lable;跳出区块,执行区块以后的代码
2.条件语句
<wbr></wbr>
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
3.循环语句
(1).while循环
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
<wbr></wbr>
(2).loop循环
[
label:
]
<wbr>LOOP<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top">statements<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"></wbr>
END
<wbr>LOOP<wbr></wbr></wbr>
[
label
]
;
<wbr></wbr>
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
<wbr></wbr>
(3).repeat until循环
[
label:
]
<wbr>REPEAT<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top">statements<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top">UNTIL<wbr>expression_r<br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"><br><img alt="" src="http://www.blue1000.com/upload/2010_01/100108171037501.gif" align="top"></wbr></wbr>
END
<wbr>REPEAT<wbr></wbr></wbr>
[
label
]
<wbr>;</wbr>
<wbr></wbr>
![](http://www.blue1000.com/upload/2010_01/100108171037501.gif)
<wbr></wbr>
五.其他常用命令
1.show procedure status
显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
2.show create procedure sp_name
显示某一个存储过程的详细信息
显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
2.show create procedure sp_name
显示某一个存储过程的详细信息
mysql存储过程中要用到的运算符
mysq存储过程中常用的函数,字符串类型操作,数学类,日期时间类。
mysql存储过程基本函数
一.字符串类<wbr></wbr>
CHARSET(str) //返回字串字符集
CONCAT (string2<wbr>[,... ]) //连接字串<br> INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0<br> LCASE (string2 ) //转换成小写<br> LEFT (string2 ,length ) //从string2中的左边起取length个字符<br> LENGTH (string ) //string长度<br> load_file (file_name ) //从文件读取内容<br> LOCATE (substring , string<wbr>[,start_position ] ) 同INSTR,但可指定开始位置<br> LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length<br> LTRIM (string2 ) //去除前端空格<br> REPEAT (string2 ,count ) //重复count次<br> REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str<br> RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length<br> RTRIM (string2 ) //去除后端空格<br> STRCMP (string1 ,string2 ) //逐字符比较两字串大小,<br> SUBSTRING (str , position<wbr>[,length ]) //从str的position开始,取length个字符,<br><span style="color:rgb(255,0,0)">注:mysql中处理字符串时,默认第一个字符下标为1</span>,即参数position必须大于等于1 <p><wbr></wbr></p> <div style="margin-left:40px">mysql> select substring(’abcd’,0,2);<br> +———————–+<br> | substring(’abcd’,0,2) |<br> +———————–+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>|<br> +———————–+<br> 1 row in set (0.00 sec) <p><wbr></wbr></p> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="margin-left:40px">mysql> select substring(’abcd’,1,2);<br> +———————–+<br> | substring(’abcd’,1,2) |<br> +———————–+<br> | ab<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>|<br> +———————–+<br> 1 row in set (0.02 sec)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <p>TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符<br> UCASE (string2 ) //转换成大写<br> RIGHT(string2,length) //取string2最后length个字符<br> SPACE(count) //生成count个空格<wbr></wbr></p> </wbr></wbr></wbr>
CONCAT (string2<wbr>[,... ]) //连接字串<br> INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0<br> LCASE (string2 ) //转换成小写<br> LEFT (string2 ,length ) //从string2中的左边起取length个字符<br> LENGTH (string ) //string长度<br> load_file (file_name ) //从文件读取内容<br> LOCATE (substring , string<wbr>[,start_position ] ) 同INSTR,但可指定开始位置<br> LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length<br> LTRIM (string2 ) //去除前端空格<br> REPEAT (string2 ,count ) //重复count次<br> REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str<br> RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length<br> RTRIM (string2 ) //去除后端空格<br> STRCMP (string1 ,string2 ) //逐字符比较两字串大小,<br> SUBSTRING (str , position<wbr>[,length ]) //从str的position开始,取length个字符,<br><span style="color:rgb(255,0,0)">注:mysql中处理字符串时,默认第一个字符下标为1</span>,即参数position必须大于等于1 <p><wbr></wbr></p> <div style="margin-left:40px">mysql> select substring(’abcd’,0,2);<br> +———————–+<br> | substring(’abcd’,0,2) |<br> +———————–+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>|<br> +———————–+<br> 1 row in set (0.00 sec) <p><wbr></wbr></p> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="margin-left:40px">mysql> select substring(’abcd’,1,2);<br> +———————–+<br> | substring(’abcd’,1,2) |<br> +———————–+<br> | ab<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>|<br> +———————–+<br> 1 row in set (0.02 sec)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <p>TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符<br> UCASE (string2 ) //转换成大写<br> RIGHT(string2,length) //取string2最后length个字符<br> SPACE(count) //生成count个空格<wbr></wbr></p> </wbr></wbr></wbr>
二.数学类
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<wbr>[,..]) //求最小值<br> MOD (numerator ,denominator ) //求余<br> POWER (number ,power ) //求指数<br> RAND([seed]) //随机数<br> ROUND (number<wbr>[,decimals ]) //四舍五入,decimals为小数位数] <p><wbr></wbr></p> <div style="margin-left:40px">注:返回类型并非均为整数,如:<br> (1)默认变为整形值<br> mysql> select round(1.23);<br> +————-+<br> | round(1.23) |<br> +————-+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>1 |<br> +————-+<br> 1 row in set (0.00 sec) <p><wbr></wbr></p> <p>mysql> select round(1.56);<br> +————-+<br> | round(1.56) |<br> +————-+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>2 |<br> +————-+<br> 1 row in set (0.00 sec)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p><span style="color:rgb(255,0,0)">(2)可以设定小数位数,返回浮点型数据</span><br> mysql> select round(1.567,2);<br> +—————-+<br> | round(1.567,2) |<br> +—————-+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>1.57 |<br> +—————-+<br> 1 row in set (0.00 sec)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <p>SIGN (number2 ) //返回符号,正负或0<br> SQRT(number2) //开平方</p> </wbr></wbr>
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<wbr>[,..]) //求最小值<br> MOD (numerator ,denominator ) //求余<br> POWER (number ,power ) //求指数<br> RAND([seed]) //随机数<br> ROUND (number<wbr>[,decimals ]) //四舍五入,decimals为小数位数] <p><wbr></wbr></p> <div style="margin-left:40px">注:返回类型并非均为整数,如:<br> (1)默认变为整形值<br> mysql> select round(1.23);<br> +————-+<br> | round(1.23) |<br> +————-+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>1 |<br> +————-+<br> 1 row in set (0.00 sec) <p><wbr></wbr></p> <p>mysql> select round(1.56);<br> +————-+<br> | round(1.56) |<br> +————-+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>2 |<br> +————-+<br> 1 row in set (0.00 sec)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p><span style="color:rgb(255,0,0)">(2)可以设定小数位数,返回浮点型数据</span><br> mysql> select round(1.567,2);<br> +—————-+<br> | round(1.567,2) |<br> +—————-+<br> |<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>1.57 |<br> +—————-+<br> 1 row in set (0.00 sec)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <p>SIGN (number2 ) //返回符号,正负或0<br> SQRT(number2) //开平方</p> </wbr></wbr>
<wbr><br><span style="font-weight:bold">三.日期时间类</span><br><wbr></wbr></wbr>
ADDTIME (date2 ,time_interval ) //将time_interval加到date2
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE (<wbr>) //当前日期<br> CURRENT_TIME (<wbr>) //当前时间<br> CURRENT_TIMESTAMP (<wbr>) //当前时间戳<br> DATE (datetime ) //返回datetime的日期部分<br> DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间<br> DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime<br> DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间<br> DATEDIFF (date1 ,date2 ) //两个日期差<br> DAY (date ) //返回日期的天<br> DAYNAME (date ) //英文星期<br> DAYOFWEEK (date ) //星期(1-7) ,1为星期天<br> DAYOFYEAR (date ) //一年中的第几天<br> EXTRACT (interval_name<wbr>FROM date ) //从date中提取日期的指定部分<br> MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串<br> MAKETIME (hour ,minute ,second ) //生成时间串<br> MONTHNAME (date ) //英文月份名<br> NOW (<wbr>) //当前时间<br> SEC_TO_TIME (seconds ) //秒数转成时间<br> STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示<br> TIMEDIFF (datetime1 ,datetime2 ) //两个时间差<br> TIME_TO_SEC (time ) //时间转秒数]<br> WEEK (date_time [,start_of_week ]) //第几周<br> YEAR (datetime ) //年份<br> DAYOFMONTH(datetime) //月的第几天<br> HOUR(datetime) //小时<br> LAST_DAY(date) //date的月的最后日期<br> MICROSECOND(datetime) //微秒<br> MONTH(datetime) //月<br> MINUTE(datetime) //分</wbr></wbr></wbr></wbr></wbr>
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE (<wbr>) //当前日期<br> CURRENT_TIME (<wbr>) //当前时间<br> CURRENT_TIMESTAMP (<wbr>) //当前时间戳<br> DATE (datetime ) //返回datetime的日期部分<br> DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间<br> DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime<br> DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间<br> DATEDIFF (date1 ,date2 ) //两个日期差<br> DAY (date ) //返回日期的天<br> DAYNAME (date ) //英文星期<br> DAYOFWEEK (date ) //星期(1-7) ,1为星期天<br> DAYOFYEAR (date ) //一年中的第几天<br> EXTRACT (interval_name<wbr>FROM date ) //从date中提取日期的指定部分<br> MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串<br> MAKETIME (hour ,minute ,second ) //生成时间串<br> MONTHNAME (date ) //英文月份名<br> NOW (<wbr>) //当前时间<br> SEC_TO_TIME (seconds ) //秒数转成时间<br> STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示<br> TIMEDIFF (datetime1 ,datetime2 ) //两个时间差<br> TIME_TO_SEC (time ) //时间转秒数]<br> WEEK (date_time [,start_of_week ]) //第几周<br> YEAR (datetime ) //年份<br> DAYOFMONTH(datetime) //月的第几天<br> HOUR(datetime) //小时<br> LAST_DAY(date) //date的月的最后日期<br> MICROSECOND(datetime) //微秒<br> MONTH(datetime) //月<br> MINUTE(datetime) //分</wbr></wbr></wbr></wbr></wbr>