MYSQL匯出一個SQL後:
DELIMITER $$
DROP TRIGGER IF EXISTS `updateegopriceondelete`$$
CREATE
TRIGGER `updateegopriceondelete` AFTER DELETE ON `customerinfo`
FOR EACH ROW BEGIN
DELETE FROM egoprice WHERE customerId=OLD.customerId;
END$$
DELIMITER ;
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 預存程序就編程這樣子了:
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>
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 中。可以運行下面的代碼建立預存程序。