MySQL 5.0 new features tutorial stored procedures: Second lecture

Source: Internet
Author: User
Tags new features

Why MySQL statements are Legal in a Procedure body

What is a MySQL statement that is legal in the stored procedure body?

What kind of SQL statement is legitimate in a MySQL stored procedure? You can create a statement that contains inserts, Update,delete, SELECT, DROP, create, replace, and so on. The only thing you need to keep in mind is that if the code contains MySQL extensions, then the code will not be ported. In standard SQL statements: Any database definition language is legal, such as:

CREATE PROCEDURE P () DELETE from T; //

SET, Commit, and rollback are also legal, such as:

CREATE PROCEDURE P () SET @x = 5; //

MySQL's additional functionality: Any data manipulation language statements will be legal.

CREATE PROCEDURE P () DROP TABLE t; //

MySQL Extensions: Direct Select is also legal:

CREATE PROCEDURE P () SELECT ' a '; //

Incidentally, I call the functionality of the DDL statement included in the stored procedure the MySQL add-on because it is defined as non-core in the SQL standard and the component can be selected.

There is a constraint in the process body that cannot have a database operation statement for a routine or table operation. For example, the following example is illegal:

CREATE PROCEDURE p1 ()
CREATE PROCEDURE p2 () DELETE FROM t; //

The following are the new statements to MySQL 5.0来 that are illegal in the process body:

Create PROCEDURE, ALTER PROCEDURE, DROP PROCEDURE, create FUNCTION,

Drop FUNCTION, CREATE TRIGGER, drop TRIGGER.

But you can use

CREATE PROCEDURE db5.p1 () DROP DATABASE db5//

But similar

"Use Database"

Statements are also illegal because MySQL assumes that the default database is the work place for the process.

Call the Procedure the stored procedure

1.

Now we can call a stored procedure, all you need to enter is called and your procedure name and one bracket again, the parentheses are necessary when you call the P1 procedure inside the example, the result is that the screen returns the contents of the T table.

mysql> CALL p1() //
+------+
| s1 |
+------+
| 5 |
+------+
1 row in set (0.03 sec)
Query OK, 0 rows affected (0.03 sec)

Because the statements in the procedure are

"SELECT * from T;"

2. Let me say that again, another way.

Other ways to implement

Mysql> call P1 ()//

As in the following statement:

Mysql> SELECT * from T; //

So, when you call the P1 process, you execute the following statement:

"SELECT * from T;"

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.