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

Summary of some common methods of JavaScript array

Table of contents 1. How to create an array in Ja...

MySQL 5.7 installation and configuration tutorial

This article shares the MySQL installation and co...

MySQL full backup and quick recovery methods

A simple MySQL full backup script that backs up t...

A brief analysis of MySQL parallel replication

01 The concept of parallel replication In the mas...

CSS: visited pseudo-class selector secret memories

Yesterday I wanted to use a:visited to change the...

How to install and configure Redis in CentOS7

Introduction There is no need to introduce Redis ...

JS implementation of carousel example

This article shares the specific code of JS to im...

mysql 5.7.19 latest binary installation

First download the zip archive version from the o...

Detailed explanation of how Node.js handles ES6 modules

Table of contents 1. Differences between the two ...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

Solution to changing the data storage location of the database in MySQL 5.7

As the data stored in the MySQL database graduall...

XHTML Getting Started Tutorial: Using the Frame Tag

<br />The frame structure allows several web...

JavaScript implements double-ended queue

This article example shares the specific code of ...

Solution to mysql server 5.5 connection failure

The solution to the problem that mysql cannot be ...