Roles of delimiter in mysql

Source: Internet
Author: User
Tags mysql tutorial

In fact, it is to tell the mysql tutorial 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)

Mysql> delimiter;

Mysql> call simpleproc (@ );
Query OK, 0 rows affected (0.00 sec)

Mysql> select @;
+ ------ +
| @ A |
+ ------ +
| 3 |
+ ------ +
1 row in set (0.00 sec)

Related Article

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.