Detailed example of using if statement in mysql stored procedure

Detailed example of using if statement in mysql stored procedure

This article uses an example to illustrate the usage of the if statement in MySQL stored procedure. Share with you for your reference, the details are as follows:

The IF statement in MySQL allows us to execute a set of SQL statements based on a certain condition or value result of an expression, so we have to form an expression in MySQL, which can be combined with text, variables, operators, and even functions. An expression can return one of three values: TRUE, FALSE, or NULL. Let's look at the grammatical structure:

IF expression THEN
  statements;
END IF;

If the above expression evaluates to TRUE, then the statements will be executed, otherwise the control flow will pass to the next statement after END IF. Let's take a look at the execution process of the IF statement:

Let's look at the syntax of the IF ELSE statement:

IF expression THEN
  statements;
ELSE
  else-statements;
END IF;

Now let's look at the execution process of the IF ELSE statement:

If we want to conditionally execute statements based on multiple expressions, we use the IF ELSE IF ELSE statement, and its syntax structure is as follows:

IF expression THEN
  statements;
ELSEIF elseif-expression THEN
  elseif-statements;
...
ELSE
  else-statements;
END IF;

If expression evaluates to TRUE, the statements in the IF branch are executed; if expression evaluates to FALSE and elseif_expression evaluates to TRUE, mysql executes elseif-expression, otherwise it executes the else-statements in the ELSE branch. Let's take a look at the specific implementation process:

We then use the IF ESLEIF ELSE statement and the GetCustomerLevel() stored procedure to accept two parameters: customer number and customer level. First, the GetCustomerLevel() stored procedure must first obtain the credit limit from the customers table. Then, based on the credit limit, it determines the customer level: PLATINUM, GOLD, and SILVER. The parameter p_customerlevel stores the customer level and is used by the calling program. Let's look at the specific SQL:

DELIMITER $$
CREATE PROCEDURE GetCustomerLevel(
  in p_customerNumber int(11),
  out p_customerLevel varchar(10))
BEGIN
  DECLARE creditlim double;
  SELECT creditlimit INTO creditlimit
  FROM customers
  WHERE customerNumber = p_customerNumber;
  IF creditlim > 50000 THEN
 SET p_customerLevel = 'PLATINUM';
  ELSEIF (creditlim <= 50000 AND creditlim >= 10000) THEN
    SET p_customerLevel = 'GOLD';
  ELSEIF creditlim < 10000 THEN
    SET p_customerLevel = 'SILVER';
  END IF;
END$$

The flow chart of the logic for determining customer level is as follows:

Okay, that’s all for this sharing.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL stored procedure skills", "MySQL common function summary", "MySQL log operation skills", "MySQL transaction operation skills summary" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Detailed explanation of creating, calling and managing MySQL stored procedures
  • Introduction to query commands for MySQL stored procedures
  • MySQL stored procedure in, out and inout parameter examples and summary
  • Detailed steps to modify MySQL stored procedures
  • Using cursor loop to read temporary table in Mysql stored procedure
  • Mysql modify stored procedure related permissions issue
  • In-depth explanation of MySQL stored procedures (in, out, inout)
  • How to create a table by month in MySQL stored procedure
  • A brief discussion on MySql views, triggers and stored procedures
  • Analysis of the advantages and disadvantages of MySQL stored procedures

<<:  jQuery realizes the picture following effect

>>:  How to open ports to the outside world in Alibaba Cloud Centos7.X

Recommend

MySQL aggregate function sorting

Table of contents MySQL result sorting - Aggregat...

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

Nginx configuration based on multiple domain names, ports, IP virtual hosts

1. Type introduction 1.1 Domain-based virtual hos...

Implementation steps for docker deployment lnmp-wordpress

Table of contents 1. Experimental Environment 2. ...

A very detailed explanation of the Linux DHCP service

Table of contents 1. DHCP Service (Dynamic Host C...

Detailed tutorial on installing mysql-8.0.20 under Linux

** Install mysql-8.0.20 under Linux ** Environmen...

How to authorize all the contents of a folder to a certain user in Linux?

【Problem Analysis】 We can use the chown command. ...

MySQL scheduled full database backup

Table of contents 1. MySQL data backup 1.1, mysql...

Detailed explanation of component development of Vue drop-down menu

This article example shares the specific code for...

Several methods of deploying multiple front-end projects with nginx

I have summarized 3 methods to deploy multiple fr...

Detailed explanation of MySQL's MERGE storage engine

The MERGE storage engine treats a group of MyISAM...

Summary of MySQL basic common commands

Table of contents MySQL basic common commands 1. ...

Instructions for using the --rm option of docker run

When the Docker container exits, the file system ...

Vue Beginner's Guide: Environment Building and Getting Started

Table of contents Initial Vue Building a Vue deve...