亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Mysql存儲過程入門

標簽:
MySQL

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 the products table.

Launch the mysql client tool and type the following commands:

DELIMITER //  CREATE PROCEDURE GetAllProducts() BEGIN    SELECT *  FROM products; END //  DELIMITER ;

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. 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 to 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 standard one.

  • 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.

  • 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 productstable.

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.

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 STORED_PROCEDURE_NAME()

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

CALL GetAllProducts();

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.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/procedure/getting-started-with-mysql-stored-procedures

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消