Roles of delimiter in MySQL

Source: Internet
Author: User
Tags mysql command line mysql gui
Source: workshop?

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)
Mysql>
ThenReturn '';
Mysql> else

Mysql>
If n <15

Mysql> then return left (S, N );
Mysql> else
Mysql> If char_length (s) <= N
Mysql>
Then 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, in the statementReturn '';MySQL interpreter will be 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, only when//This statement is executed by the MySQL interpreter only after it 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 (@ );
Query OK, 0 rows affected (0.00 Sec)

Mysql> select @;
+ ------ +
| @ 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 that counts Website access (user agent. Is the following SQL code.

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 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:

Delimiter //; -- change MySQL delimiter to "//" 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 back 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: "//" 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 it back 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 ";" mysql> drop procedure if exists pr_stat_agent // query OK, 0 rows affected (0.00 Sec) mysql> -- call pr_stat_agent ('1970-07-17 ', '1970-07-18') 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> delimiter; -- do not end with the symbol "//" 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.

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 command is "\."

mysql> \. d:\pr_stat_agent.sqlQuery OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)

Finally, we can see that the mysql client tool is different in some places.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.