A2-06-02. Getting Started with MySQL Stored Procedures

轉載自:http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspxmysql

 

Getting Started with MySQL Stored Procedures

 

Summaryin this tutorial, we will show you step by step how to develop the first MySQL stored procedure using CREATE PROCEDURE statement. In addition, we will show you how to call stored procedures from SQL statements.
sql

Writing the first MySQL stored procedure

We are going to develop a simple stored procedure named GetAllProducts()  to help you get familiar with the syntax. The GetAllProducts()  stored procedure selects all products from the products  table.app

Launch the mysql client tool and type the following commands:oop

Creae MySQL stored procedure using command-line tool

Let’s examine the stored procedure in greater detail:post

  • The first command is DELIMITER // , which is not related to the stored procedure syntax. The DELIMITER statement changes the standard delimiter which is semicolon ( ; ) to another. In this case, the delimiter is changed from the semicolon( ; ) to double-slashes //. Why do we have to change the delimiter? Because we want to pass the stored procedure to the server as a whole rather than letting mysql tool interpret each statement at a time.  Following the END keyword, we use the delimiter //  to indicate the end of the stored procedure. The last command ( DELIMITER;) changes the delimiter back to the semicolon (;).
  • We use the CREATE PROCEDURE  statement to create a new stored procedure. We specify the name of stored procedure after the CREATE PROCEDURE  statement. In this case, the name of the stored procedure is GetAllProducts We put the parentheses after the name of the stored procedure.
  • The section between BEGIN and END  is called the body of the stored procedure. You put the declarative SQL statements in the body to handle business logic. In this stored procedure, we use a simple SELECT statement to query data from the products table.

It is tedious to write the stored procedure in mysql client tool, especially when the stored procedure is complex. Most of the GUI tools for MySQL allow you to create new stored procedures via an intuitive interface.ui

For example, in MySQL Workbench, you can create a new stored procedure as follows:this

First, right-click on the Routines and choose the Create Procedure… menu item.spa

create-mysql-stored-procedure-mysql-workbench-step-1

Next, enter the stored procedure code and click the Apply buttoncode

Create MySQL Stored Procedure using MySQL Workbench Step 2

Then, you can review the code before MySQL stores it in the database. Click the Apply button if everything is good.server

Create MySQL Stored Procedure using MySQL Workbench Step 2 - Review

After that, MySQL compiles and puts the stored procedure in the database catalog; click the Finishbutton.

Create MySQL Stored Procedure using MySQL Workbench Step 3

Finally, you can see a new stored procedure created under Routines of the classicmodels database.

Create MySQL Stored Procedure using MySQL Workbench Final Step

We have created a new stored procedure. Now, it’s time to learn how to use it.

Calling stored procedures

In order to call a stored procedure, you use the following SQL command:

You use the CALL  statement to call a stored procedure e.g., to call the GetAllProducts() stored procedure, you use the following statement:

If you execute the statement above, you will get all products in the products table.
Call MySQL Stored Procedure
In this tutorial, you have learned how to write a simple stored procedure using the CREATE PROCEDURE statement and call it from an SQL statement using the CALL statement.

相關文章
相關標籤/搜索