Getting Started with MySQL Stored Procedures

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

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 theproducts table.

开发一个简单的存储过程GetAllProducts()帮助熟悉存储过程的语法,该函数返回表products的所有的产品。

Launch the mysql client tool and type the following commands:

Creae MySQL stored procedure using command-line tool

Let’s examine the stored procedure in greater detail:

  • The first command is DELIMITER //, which is not related to the stored procedure syntax. TheDELIMITER 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 to interpret each statement at a time.  Following the ENDkeyword, we use the delimiter // to indicate the end of the stored procedure. The last command (DELIMITER;) changes the delimiter back to the standard one.
  • 第一个命令DELIMITER//,与存储过程的语法无关。该声明改变标准的分隔符分号为其他符号。在这个例子中,分隔符从分号变为双斜杠。为什么改变分隔符?因为我们希望将存储过程作为一个整体传送给服务器,而不是让mysql工具翻译每个语句。在END命令后,我们使用//暗示存储过程的结束。最后DELIMITER;将分隔符变为标准分隔符。
  • 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 GetAllProductsWe put the parentheses after the name of the stored procedure.
  • 使用CREATE PROCEDURE 声明创建一个新的存储过程。在create procedure 后指定存储过程名称。在这个例子中,存储过程的名称为GetAllProducts。在存储过程名称后添加括号。
  • 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’s kind of 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.

在mysql客户端工具中写存储过程是冗长乏味的,特别是复杂的存储过程。大多数mysql的图形用户界面工具可以直观的创建存储过程

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

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

Right mouse click on the Routines and choose “Create Procedure…”

Create MySQL Stored Procedure using MySQL Workbench Step 2

Enter the stored procedure code and click the Apply button

Create MySQL Stored Procedure using MySQL Workbench Step 2 - Review

You can review the code before MySQL stores it in the database. Click Apply button if everything is good.

Create MySQL Stored Procedure using MySQL Workbench Step 3

MySQL compiles and puts the stored procedure in the database catalog; click the Finish button.

Create MySQL Stored Procedure using MySQL Workbench Final Step

You can see a new stored procedure created under Routines of the classicmodelsdatabase

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:

调用存储过程,可以使用 call 存储过程名称()

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 by using the CREATE PROCEDURE statement and call it by using the CALL statement.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值