What is the role of MySqldelimiter? bitsCN. comMYSQL after exporting an SQL statement:
DELIMITER $
Drop trigger if exists 'updateegopriceondelete' $
CREATE
TRIGGER 'updateegopriceondelete' after delete on 'mermerinfo'
FOR EACH ROW BEGIN
Delete from egoprice WHERE customerId = OLD. customerId;
END $
DELIMITER;
Set the DELIMITER Terminator to "$", and then define it as ";". the default Terminator of MYSQL is ";".
Explanation:
In fact, it is to tell the mysql interpreter whether the command has ended and whether mysql can be executed.
By default, delimiter is a semicolon ;. In the command line client, if a command line ends with a semicolon,
After you press enter, mysql will execute this command. Enter the following statement.
Mysql> select * from test_table;
Press enter, and MySQL will immediately execute the statement.
But sometimes, you don't want MySQL to do this. The statement contains a semicolon.
If you try 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 <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 impossible for the user to execute the entire statement after entering all these statements.
Mysql runs automatically when it encounters a semicolon.
That is, when the statement RETURN '';, the mysql interpreter is executed.
In this case, you need to replace delimiter with 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 ;//
In this way, the mysql interpreter will 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)
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 on the MySQL graphical client EMS SQL Manager 2005 for MySQL. However, an error occurs on the SQLyog MySQL GUI v5.02 client. The final reason is that delimiter is not properly configured. By default, delimiter ";" is used to Submit Query statements to MySQL. In the stored procedure, each SQL statement ends with a ";". if ";" is submitted to MySQL at this time, a problem may occur. So I changed MySQL's delimiter. the above MySQL stored procedure was programmed like this:
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 to the default MySQL delimiter: ";"
Of course, the MySQL delimiter symbol can be set freely. you can use "/" or "$. However, the common usage of MySQL stored procedures is "//" and "$ ". The above code in SQLyog is moved to the MySQL Command Client (MySQL Command Line Client) but cannot be executed.
Mysql> delimiter //; -- change MySQL delimiter to "//"
Mysql>
Mysql> drop procedure if exists pr_stat_agent //
->
-> -- Call pr_stat_agent ('2017-07-17 ', '2017-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 to the default MySQL delimiter: ";"
-> //
-> //
-> //
->;
->;
->
It's strange! Finally, we finally found the problem. if we run "delimiter //;" on the MySQL command line, MySQL's delimiter is actually "//;" instead of what we expected "/". In fact, you only need to run the command "delimiter.
Mysql> delimiter // -- do not mark ";" 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 ('2017-07-17 ', '2017-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 mark "//" at the end
Mysql>
We can execute the SQL code in the file in the MySQL database. For example, I put the stored procedure code in the file d:/pr_stat_agent. SQL. You can run the following code to create a stored procedure.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.