The role of delimiter in MySQL

Source: Internet
Author: User
Tags mysql tutorial

In fact, tell the MySQL tutorial interpreter, whether the command is over, whether MySQL can be executed.
By default, delimiter is a semicolon;. In a command-line client, if a line of commands ends with a semicolon,
Then, after the carriage return, MySQL will execute the command. If you enter the following statement
Mysql> select * from test_table;
And then enter, then MySQL will execute the statement immediately.

But sometimes, you don't want MySQL to do this. A statement containing a semicolon is included in the statement for which you may enter more.
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 to left (S, N);
Mysql> Else
mysql> if Char_length (s) <=n then
Mysql> return s;
Mysql> Else
Mysql> return concat (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 completed all of these statements and then execute the entire paragraph.
Because MySQL once encountered a semicolon, it will automatically execute.
That is, the MySQL interpreter will execute when the statement return ';
In this case, you need to replace delimiter with other symbols, 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 to left (S, N);
Mysql> Else
mysql> if Char_length (s) <=n then
Mysql> return s;
Mysql> Else
Mysql> return concat (S, n-10), ' ... ', right (s, 5));
Mysql> End If;
Mysql> End If;
Mysql> end;//
This is the only time the MySQL interpreter executes this statement when//appears.


Example:

Mysql> delimiter//

Mysql> CREATE PROCEDURE Simpleproc (out param1 int)
-> begin
-> Select COUNT (*) into the 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)

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.