Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What exactly is the mysql stored procedure?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly gives you a brief introduction to what mysql stored procedures are like. You can check the relevant professional terms on the Internet or find some related books to supplement them. We will not dabble here, so let's go straight to the topic. I hope this article on the specific nature of mysql stored procedures can bring you some practical help.

Stored procedure:

A stored procedure is a collection of SQL statements stored in a database. Stored procedures can contain business logic, which is one of the keys to distinguishing stored procedures from views. In addition, stored procedures can also accept parameters, we can set variables in stored procedures, write statements, and so on.

How stored procedures work

First create a stored procedure and then run it. During the run, you need to provide the parameters it needs, and then the stored procedure will be executed using the parameters in any way specified by the code. For example, write a stored procedure that accepts FruitId parameters. You can then get this parameter in the stored procedure and use it to check the inventory of that particular fruit. So we can call the stored procedure, that is, each time we use a different fruit ID, it will return a value and show how many fruits are in that inventory.

Create a stored procedure

We can create stored procedures through CREATE PROCEDURE statements

CREATE PROCEDURE demo_name (pendant 1 INT) BEGIN... code goes here...END

Demo_name refers to the name of the stored procedure. Parentheses are required and can be empty if they do not contain any parameters.

The body of the stored procedure is between the BEGIN and END keywords. These keywords are used to write compound statements. Compound statements can contain multiple statements, which can be nested if necessary.

Example: create a stored procedure named FruitStock:

DELIMITER / / CREATE PROCEDURE FruitStock (thisFruit SMALLINT) BEGIN SELECT Fruit.FruitName, Fruit.Inventory, Units.UnitName FROM Fruit INNER JOIN Units ON Fruit.UnitId = Units.UnitId WHERE Fruit.FruitId = thisFruit;END / / DELIMITER

After you have created a stored procedure, you will call the stored procedure

Call an ID with an argument of 1

CALL FruitStock (1)

Delete stored procedure

You can use the DROP PROCEDURE statement to delete a stored procedure.

DROP PROCEDURE FruitStock

Change the stored procedure

The stored procedure ALTER PROCEDURE can be changed in a stored procedure through the following statements.

It is important to note that when you want to change the body of a stored procedure or any of its parameters, you need to delete the procedure and create it again

Example: add the list to be returned by Fruit.FruitId

DROP PROCEDURE IF EXISTS FruitStock;DELIMITER / / CREATE PROCEDURE FruitStock (thisFruit SMALLINT) BEGIN SELECT Fruit.FruitId, Fruit.FruitName, Fruit.Inventory, Units.UnitName FROM Fruit INNER JOIN Units ON Fruit.UnitId = Units.UnitId WHERE Fruit.FruitId = thisFruit;END / / DELIMITER

Mysql stored procedure is exactly what it is to tell you here, for other related issues you want to know can continue to pay attention to our industry information. Our section will capture some industry news and professional knowledge to share with you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report