The following article is mainly to describe what is the role of delimiter in MySQL database? We generally think that this command is not related to stored procedures, is this the case? The following articles will give you the relevant knowledge, hope you will have something to gain.
In fact, just tell the MySQL interpreter if the command is over and the MySQL database is ready to execute. By default, delimiter is a semicolon;. In the command-line client, if a single line of commands ends with a semicolon, the command will be executed by MySQL when the carriage returns. 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> RETURN ';
- MySQL> ELSEIF N< 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 entered all of these statements before executing the entire statement. Since MySQL encounters a semicolon, it will be executed automatically. That is, the MySQL database interpreter will execute when the statement return '; In this case, the delimiter must be replaced with other symbols such as//or $$ in advance.
- 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< 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//appears
Example:
- MySQL> Delimiter//
- MySQL> CREATE PROCEDURE simpleproc (out param1 INT)
- -> BEGIN
- -> SELECT COUNT (*) into 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 database 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 Agent
- 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 ')
- 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;
Change back to the default MySQL delimiter: ";"
Of course, MySQL delimiter symbols can be set freely, you can use "/" or "$$" and so on. But the more common usage in MySQL database storage 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 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;
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_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;
Do not 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.sql
- Query 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.sql
- Query OK, 0 rows Affected (0.00 sec)
- Query OK, 0 rows Affected (0.00 sec)
Finally, the client tools for the MySQL database are visible in some places, each with its own set.
Overview of the role of delimiter in MySQL database