三.日期时间类
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
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) //分
DAY ,DAY_HOUR ,DAY_MINUTE ,DAY_SECOND ,HOUR ,HOUR_MINUTE ,HOUR_SECOND ,MINUTE ,MINUTE_SECOND,MONTH ,SECOND ,YEAR
DECLARE variable_name [,variable_name...] datatype [DEFAULT value]; |
其中,datatype为mysql的数据类型,如:INT, FLOAT, DATE, VARCHAR(length)
例:
DECLARE l_int INT unsigned default 4000000; DECLARE l_numeric NUMERIC(8,2) DEFAULT 9.95; DECLARE l_date DATE DEFAULT '1999-12-31'; DECLARE l_datetime DATETIME DEFAULT '1999-12-31 23:59:59'; DECLARE l_varchar VARCHAR(255) DEFAULT 'This will not be padded';
变量赋值
SET 变量名 = 表达式值 [,variable_name = expression ...]
参数
mysql存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT
Create procedure|function([[IN |OUT |INOUT ] 参数名数据类形...]) |
IN 输入参数
表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值
OUT 输出参数
该值可在存储过程内部被改变,并可返回
INOUT 输入输出参数
调用时指定,并且可被改变和返回
IN参数例子:
创建
mysql> CREATE PROCEDURE sp_demo_in_parameter(IN p_in INT) BEGIN SELECT p_in; */ SET p_in=2; select p_in; END;
执行结果:
mysql> set @p_in=1
mysql> call sp_demo_in_parameter(@p_in)
+------+
| p_in |
+------+
|
+------+
+------+
| p_in |
+------+
|
+------+
mysql> select @p_in;
+-------+
| @p_in |
+-------+
| 1
+-------+
以上可以看出,p_in虽然在存储过程中被修改,但并不影响@p_id的值
OUT参数例子
创建:
mysql> CREATE PROCEDURE sp_demo_out_parameter(OUT p_out INT) BEGIN SELECT p_out; SET p_out=2; SELECT p_out; END;
执行结果:
mysql> SET @p_out=1
mysql> CALL sp_demo_out_parameter(@p_out)
+-------+
| p_out |
+-------+
|
+-------+
+-------+
| p_out |
+-------+
|
+-------+
mysql> SELECT @p_out;
+-------+
| p_out |
+-------+
|
+----------+
INOUT参数例子:
mysql> CREATE PROCEDURE sp_demo_inout_parameter(INOUT p_inout INT) BEGIN SELECT p_inout; SET p_inout=2; SELECT p_inout; END;
执行结果:
set @p_inout=1
call sp_demo_inout_parameter(@p_inout) //
+---------+
| p_inout |
+---------+
|
+---------+
+---------+
| p_inout |
+---------+
|
+---------+
select @p_inout;
+----------+
| @p_inout |
+----------+
| 2
+----------+
用户变量
1、
mysql> SELECT 'Hello World' into @x;
mysql> SELECT @x;
+-------------+
| @x
+-------------+
| Hello World |
+-------------+
mysql> SET @y='Goodbye Cruel World';
mysql> select @y;
+---------------------+
| @y
+---------------------+
| Goodbye Cruel World |
+---------------------+
mysql> SET @z=1+2+3;
mysql> select @z;
+------+
| @z
+------+
| 6
+------+
2、
mysql> CREATE PROCEDURE GreetWorld(
mysql> SET @greeting='Hello';
mysql> CALL GreetWorld(
+----------------------------+
| CONCAT(@greeting,' World') |
+----------------------------+
| Hello World
+----------------------------+
3、
mysql> CREATE PROCEDURE p1(
mysql> CREATE PROCEDURE p2(
mysql> CALL p1(
mysql> CALL p2(
+-----------------------------------------------+
| CONCAT('Last procedure was ',@last_procedure) |
+-----------------------------------------------+
| Last procedure was p1
+-----------------------------------------------+
注意:
①用户变量名一般以@开头
②滥用用户变量会导致程序难以理解及管理
注释
mysql存储过程可使用两种风格的注释
双模杠:--
该风格一般用于单行注释
c风格: 一般用于多行注释
例子:
-------------------------------------------------------------------
CREATE PROCEDURE `SecondProdure`(in sid int,out areaname varchar(10))
java调用存储过程
引用地址:http://blog.163.com/xiao_maomao_chong/blog/static/31686239200792015422985/