改回默认的 MySQL delimiter:“;”
- -> //
- -> //
- -> //
- -> ;
- -> ;
- ->
真是奇怪了!最后终于发现问题了,在 MySQL 命令行下运行 “delimiter //; ” 则 MySQL 的 delimiter 实际上是 “//;”,而不是我们所预想的 “//”。其实只要运行指令 “delimiter //” 就 OK 了。
MySQL> delimiter // -- 末尾不要符号 “;”
- MySQL>
- MySQL> drop procedure if exists pr_stat_agent //
- Query OK, 0 rows affected (0.00 sec)
- MySQL>
- MySQL> -- call pr_stat_agent ('2008-07-17', '2008-07-18')
- MySQL>
- MySQL> create procedure pr_stat_agent
- -> (
- -> pi_date_from date
- -> ,pi_date_to date
- -> )
- -> begin
- -> -- check input
- -> if (pi_date_from is null) then
- -> set pi_date_from = current_date();
- -> end if;
- ->
- -> if (pi_date_to is null) then
- -> set pi_date_to = pi_date_from;
- -> end if;
- ->
- -> set pi_date_to = date_add(pi_date_from, interval 1 day);
- ->
- -> -- stat
- -> select agent, count(*) as cnt
- -> from apache_log
- -> where request_time >= pi_date_from
- -> and request_time < pi_date_to
- -> group by agent
- -> order by cnt desc;
- -> end; //
- Query OK, 0 rows affected (0.00 sec)
- MySQL>
- MySQL> delimiter ;
末尾不要符号 “//”
- MySQL>
顺带一提的是,我们可以在 MySQL 数据库中执行在文件中的 SQL 代码。例如,我把上面存储过程的代码放在文件 d:\pr_stat_agent.sql 中。可以运行下面的代码建立存储过程。
- MySQL> source d:\pr_stat_agent.sql
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
source 指令的缩写形式是:“\.”
- MySQL> \. d:\pr_stat_agent.sql
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
最后,可见 MySQL数据库的客户端工具在有些地方是各自为政,各有各的一套。