This command doesn't have anything to do with stored procedures.
In fact, just tell the MySQL interpreter if the command is over and MySQL can execute it.
By default, delimiter is a semicolon;。 In the command-line client, if a single line of commands ends with a semicolon,
Then after the carriage return, MySQL will execute the command. such as entering the following statement
mysql> SELECT * from test_table;
Then enter, then MySQL will execute the statement immediately.
But sometimes, you don't want MySQL to do this. More statements are entered for the possible, and the statement contains a semicolon.
If you attempt to enter the following statement in the command line client
mysql> CREATE FUNCTION ' SHORTEN ' (S VARCHAR (255), N INT)
mysql> RETURNS varchar (255)
mysql> BEGIN
mysql> IF ISNULL (S) then
mysql> < Strong>return ';
mysql> ELSEIF n<15 then
mysql> RETURN Left (S, N);
mysql> ELSE
mysql> IF char_length (S) <=n then
mysql> RETURN S;
mysql> ELSE
mysql> RETURN CONCAT (left (s, N-10), ' ... ', right (s, 5));
mysql> END IF;
Mysql> END IF;
Mysql> END;
By default, it is not possible to wait until the user has finished entering all these statements before executing the entire statement.
since MySQL encounters a semicolon, it will be executed automatically.
that is, in the statement RETURN '; , the MySQL interpreter is going to execute.
In this case, you need to change the delimiter to another symbol, such as//or $$.
Mysql> delimiter//
mysql> CREATE FUNCTION ' SHORTEN ' (S VARCHAR (255), N INT)
mysql> RETURNS varchar (255)
Mysql> BEGIN
Mysql> IF ISNULL (S) Then
mysql> RETURN ';
Mysql> ELSEIF N<15 Then
Mysql> RETURN Left (S, N);
Mysql> ELSE
Mysql> IF char_length (S) <=n Then
Mysql> RETURN S;
Mysql> ELSE
Mysql> RETURN CONCAT (left (s, N-10), ' ... ', right (s, 5));
Mysql> END IF;
Mysql> END IF;
Mysql> END; //
This allows the MySQL interpreter to execute this statement only when // after it appears
Example:
Mysql> delimiter//
Mysql> CREATE PROCEDURE Simpleproc (out param1 INT)
BEGIN
SELECT COUNT (*) to param1 from T;
-END;
//
Query OK, 0 rows Affected (0.00 sec)
Mysql> delimiter;
Mysql> call Simpleproc (@a);
Query OK, 0 rows Affected (0.00 sec)
Mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in Set (0.00 sec)
The code in this article runs through MySQL 5.0.41-community-nt.
Compiled a MySQL stored procedure for the user agent of the statistics website. This is the SQL code below.
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 Agents ORDER by CNT Desc;end;
I can run smoothly under EMS SQL Manager 2005 for MySQL this MySQL graphics client. But in SQLyog MySQL GUI v5.02 This client will go wrong. The last reason to find out is the problem of not setting delimiter. By default, delimiter ";" is used to submit query statements to MySQL. There is a ";" at the end of each SQL statement in the stored procedure, and if so, it will be a problem if you submit it to MySQL every time. So change the MySQL delimiter, the above MySQL stored procedure is programmed this way:
delimiter//; --Change MySQL delimiter to: "//" drop procedure if exists pr_stat_agent//--call pr_stat_agent (' 2008-07-17 ', ' 2008-07-18 ') CRE Ate 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 Agents ORDER by CNT Desc;end;//delimiter;// --Change back to default MySQL del Imiter: ";"
Of course, MySQL delimiter symbols can be set freely, you can use "/" or "$$" and so on. But the more common use of MySQL stored procedures is "//" and "$$". The above-mentioned code in SQLYOG is not executed by the MySQL command line client.
Mysql> delimiter//; --Change MySQL delimiter to: "//" 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 n ULL) 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 Reque St_time >= Pi_date_from, and Request_time < pi_date_to, GROUP by agent, order by CNT Desc; -End; delimiter; --Change back to the default MySQL deliMiter: ";" ,///-/---- ; -
It's strange! Finally found the problem, the MySQL command line run "delimiter//;" Then the MySQL delimiter is actually "//;" Instead of "//" as we expected. In fact, just run the command "delimiter//" is OK.
Mysql> delimiter//--no sign at the end ";" 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_dat E_from date, pi_date_to date, Begin--check input, if (pi_date_f ROM is null) and 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 Reque St_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; --no sign at the end "//" mysql>
Incidentally, we can execute the SQL code in the file in the MySQL database. For example, I put the code for the stored procedure above in the file D:\pr_stat_agent.sql. You can run the following code to establish a stored procedure.
Mysql> source D:\pr_stat_agent.sqlQuery OK, 0 rows Affected (0.00 sec) Query OK, 0 rows Affected (0.00 sec)
The abbreviated form of the source directive is: "\."
mysql> \. D:\pr_stat_agent.sqlQuery OK, 0 rows Affected (0.00 sec) Query OK, 0 rows Affected (0.00 sec)
Finally, MySQL's client tools are visible in some places, each with its own set.
MySQL--delimiter