Get the App
SLTechnology News&Howtos  ›  Database  › 

What exactly is the mysql stored procedure?

Shulou Source: shulou.com Published: 2022-06-01 18:35:28 09月17日 Update

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.

Tags: Procedure storage statement parameter key fruit specialty subject keyword inventory parenthesis industry run different next expertise business subject between books Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS MariaDB OPPO Reno NVidia