MySQL:在命令行下创建 MySQL 存储过程有关问题
来源:互联网 发布:淘宝引流推广软件 编辑:程序博客网 时间:2024/05/17 23:38
MySQL:在命令行下创建 MySQL 存储过程问题
ref/blog/static/8614152320081010103521768/
本文代码在 MySQL 5.0.41-community-nt 下运行通过。
编写了个统计网站访问情况(user agent)的 MySQL 存储过程。就是下面的这段 SQL 代码。
drop procedure if exists pr_stat_agent;
-- call pr_stat_agent ('2008-07-17', '2008-07-18')
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;
我在 EMS SQL Manager 2005 for MySQL 这个 MySQL 图形客户端下可以顺利运行。但是在 SQLyog MySQL GUI v5.02 这个客户端就会出错。最后找到原因是没有设置好 delimiter 的问题。默认情况下,delimiter “;” 用于向 MySQL 提交查询语句。在存储过程中每个 SQL 语句的结尾都有个 “;”,如果这时候,每逢 “;” 就向 MySQL 提交的话,当然会出问题了。于是更改 MySQL 的 delimiter,上面 MySQL 存储过程就编程这样子了:
delimiter //; -- 改变 MySQL delimiter 为:“//”
drop procedure if exists pr_stat_agent //
-- call pr_stat_agent ('2008-07-17', '2008-07-18')
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; //
delimiter ; // -- 改回默认的 MySQL delimiter:“;”
当然,MySQL delimiter 符号是可以自由设定的,你可以用 “/” 或者“$$” 等。但是 MySQL 存储过程中比较常见的用法是 “//” 和 “$$”。上面的这段在 SQLyog 中的代码搬到 MySQL 命令客户端(MySQL Command Line Client)却不能执行。
mysql> delimiter //; -- 改变 MySQL delimiter 为:“//”
mysql>
mysql> drop procedure if exists pr_stat_agent //
->
-> -- call pr_stat_agent ('2008-07-17', '2008-07-18')
->
-> 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; //
->
-> delimiter ; // -- 改回默认的 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)
ref/blog/static/8614152320081010103521768/
本文代码在 MySQL 5.0.41-community-nt 下运行通过。
编写了个统计网站访问情况(user agent)的 MySQL 存储过程。就是下面的这段 SQL 代码。
drop procedure if exists pr_stat_agent;
-- call pr_stat_agent ('2008-07-17', '2008-07-18')
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;
我在 EMS SQL Manager 2005 for MySQL 这个 MySQL 图形客户端下可以顺利运行。但是在 SQLyog MySQL GUI v5.02 这个客户端就会出错。最后找到原因是没有设置好 delimiter 的问题。默认情况下,delimiter “;” 用于向 MySQL 提交查询语句。在存储过程中每个 SQL 语句的结尾都有个 “;”,如果这时候,每逢 “;” 就向 MySQL 提交的话,当然会出问题了。于是更改 MySQL 的 delimiter,上面 MySQL 存储过程就编程这样子了:
delimiter //; -- 改变 MySQL delimiter 为:“//”
drop procedure if exists pr_stat_agent //
-- call pr_stat_agent ('2008-07-17', '2008-07-18')
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; //
delimiter ; // -- 改回默认的 MySQL delimiter:“;”
当然,MySQL delimiter 符号是可以自由设定的,你可以用 “/” 或者“$$” 等。但是 MySQL 存储过程中比较常见的用法是 “//” 和 “$$”。上面的这段在 SQLyog 中的代码搬到 MySQL 命令客户端(MySQL Command Line Client)却不能执行。
mysql> delimiter //; -- 改变 MySQL delimiter 为:“//”
mysql>
mysql> drop procedure if exists pr_stat_agent //
->
-> -- call pr_stat_agent ('2008-07-17', '2008-07-18')
->
-> 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; //
->
-> delimiter ; // -- 改回默认的 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)
0 0
- MySQL:在命令行下创建 MySQL 存储过程有关问题
- MySQL:在命令行下创建 MySQL 存储过程问题
- MYSQL在命令行创建存储过程
- mysql 命令行创建存储过程
- MYSQL命令行里创建存储过程(初级)
- 图解MySql命令行创建存储过程
- mysql命令行下处理存储过程
- 如何在mysql 6.0.2下创建存储过程?
- linux系统下无法创建mysql存储过程问题
- linux系统下无法创建mysql存储过程问题
- mysql 创建存储过程权限问题
- MYSQL命令行创建存储过程分号的解决方法
- mysql创建存储过程
- mysql创建存储过程
- mysql创建存储过程
- mysql创建存储过程
- mysql创建存储过程
- mysql存储过程创建
- 用虚拟机安装win10全程图文教程(超详细)
- URAL 1152 False Mirrors 搜索|记忆化搜索|状压
- 结构体与类的区别
- WebApp 的 meta 标签
- MySQL学习2
- MySQL:在命令行下创建 MySQL 存储过程有关问题
- RIoTboard连载五 RIoTboard GPIO相关函数和宏定义简介
- AndroidDevTools简介&Android SDK在线更新镜像服务器
- 云计算如今已经发展到什么阶段?
- curl方式发送数据 cuel命令
- UITableView 分割线不靠左(未补全),删除多余分割线问题
- DNA逆序对的问题: DNA sorting
- java.lang.RuntimeException: Unable to instantiate activity
- C#.NET和SQL Server中如何生成GUID