龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > MySQL 技术 >

MySQL数据库中delimiter的作用概述(1)(2)

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
默认情况下,delimiter ; 用于向 MySQL 提交查询语句。在存储过程中每个 SQL 语句的结尾都有个 ;,如果这时候,每逢 ; 就向 MySQL 提交的话,当然会出问题了。

默认情况下,delimiter “;” 用于向 MySQL 提交查询语句。在存储过程中每个 SQL 语句的结尾都有个 “;”,如果这时候,每逢 “;” 就向 MySQL 提交的话,当然会出问题了。于是更改 MySQL 的 delimiter,上面 MySQL 存储过程就编程这样子了:

delimiter //; -- 改变 MySQL delimiter 为:“//”

  1. drop procedure if exists pr_stat_agent //  
  2. -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  3. create procedure pr_stat_agent  
  4. (  
  5. pi_date_from date  
  6. ,pi_date_to date  
  7. )  
  8. begin  
  9. -- check input  
  10. if (pi_date_from is null) then  
  11. set pi_date_from = current_date();  
  12. end if;  
  13. if (pi_date_to is null) then  
  14. set pi_date_to = pi_date_from;  
  15. end if;  
  16. set pi_date_to = date_add(pi_date_from, interval 1 day);  
  17. -- stat  
  18. select agent, count(*) as cnt  
  19. from apache_log  
  20. where request_time >= pi_date_from  
  21. and request_time < pi_date_to 
  22. group by agent  
  23. order by cnt desc;  
  24. end; //  
  25. delimiter ;  

改回默认的 MySQL delimiter:“;”

当然,MySQL delimiter 符号是可以自由设定的,你可以用 “/” 或者“$$” 等。但是 MySQL数据库 存储过程中比较常见的用法是 “//” 和 “$$”。上面的这段在 SQLyog 中的代码搬到 MySQL 命令客户端(MySQL Command Line Client)却不能执行。

MySQL> delimiter //; -- 改变 MySQL delimiter 为:“//”

  1. MySQL> 
  2. MySQL> drop procedure if exists pr_stat_agent //  
  3. -> 
  4. -> -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  5. -> 
  6. -> create procedure pr_stat_agent  
  7. -> (  
  8. -> pi_date_from date  
  9. -> ,pi_date_to date  
  10. -> )  
  11. -> begin  
  12. -> -- check input  
  13. -> if (pi_date_from is null) then  
  14. -> set pi_date_from = current_date();  
  15. -> end if;  
  16. -> 
  17. -> if (pi_date_to is null) then  
  18. -> set pi_date_to = pi_date_from;  
  19. -> end if;  
  20. -> 
  21. -> set pi_date_to = date_add(pi_date_from, interval 1 day);  
  22. -> 
  23. -> -- stat  
  24. -> select agent, count(*) as cnt  
  25. -> from apache_log  
  26. -> where request_time >= pi_date_from  
  27. -> and request_time < pi_date_to 
  28. -> group by agent  
  29. -> order by cnt desc;  
  30. -> end; //  
  31. -> 
  32. -> delimiter ; 

改回默认的 MySQL delimiter:“;”

  1. -> //  
  2. -> //  
  3. -> //  
  4. -> ;  
  5. -> ;  
  6. -> 

真是奇怪了!最后终于发现问题了,在 MySQL 命令行下运行 “delimiter //; ” 则 MySQL 的 delimiter 实际上是 “//;”,而不是我们所预想的 “//”。其实只要运行指令 “delimiter //” 就 OK 了。

MySQL> delimiter // -- 末尾不要符号 “;”

  1. MySQL> 
  2. MySQL> drop procedure if exists pr_stat_agent //  
  3. Query OK, 0 rows affected (0.00 sec)  
  4. MySQL> 
  5. MySQL> -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  6. MySQL> 
  7. MySQL> create procedure pr_stat_agent  
  8. -> (  
  9. -> pi_date_from date  
  10. -> ,pi_date_to date  
  11. -> )  
  12. -> begin  
  13. -> -- check input  
  14. -> if (pi_date_from is null) then  
  15. -> set pi_date_from = current_date();  
  16. -> end if;  
  17. -> 
  18. -> if (pi_date_to is null) then  
  19. -> set pi_date_to = pi_date_from;  
  20. -> end if;  
  21. -> 
  22. -> set pi_date_to = date_add(pi_date_from, interval 1 day);  
  23. -> 
  24. -> -- stat  
  25. -> select agent, count(*) as cnt  
  26. -> from apache_log  
  27. -> where request_time >= pi_date_from  
  28. -> and request_time < pi_date_to 
  29. -> group by agent  
  30. -> order by cnt desc;  
  31. -> end; //  
  32. Query OK, 0 rows affected (0.00 sec)  
  33. MySQL> 
  34. MySQL> delimiter ;   

末尾不要符号 “//”

  1. MySQL> 

顺带一提的是,我们可以在 MySQL 数据库中执行在文件中的 SQL 代码。例如,我把上面存储过程的代码放在文件 d:pr_stat_agent.sql 中。可以运行下面的代码建立存储过程。

  1. MySQL> source d:pr_stat_agent.sql  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. Query OK, 0 rows affected (0.00 sec)  

source 指令的缩写形式是:“.”

  1. MySQL> . d:pr_stat_agent.sql  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. Query OK, 0 rows affected (0.00 sec)  

最后,可见 MySQL数据库的客户端工具在有些地方是各自为政,各有各的一套。


精彩图集

赞助商链接