SQL
  • Tips
  • SQL
    • Database Basics
    • SQL Basics
    • SQL Syntax
    • Retrieve Data: SELECT
    • Sort Data: ORDER BY
    • Filter Data: WHERE
    • Calculated Fields
    • Aggregate Functions
    • Group Data: GROUP BY
    • Subqueries
    • Join Tables
    • Combine Queries: UNION
    • Control Flow Statements
    • IF function
    • Handle NULL
    • Date
    • Numeric
    • String
    • Notes
  • Table/Database
    • Insert
    • Delete
    • Update
    • Table
    • Database
    • Stored Procedure
  • Misc
    • SQL vs NoSQL
    • 大数据
    • Why SQL instead of Excel + VBA?
  • sqlzoo
    • world table
    • nobel table
    • football data
    • movie data
    • Teacher Department Data
    • Edinburgh Buses data
  • Leetcode
    • 175_Combine Two Tables
    • 176_Second Highest Salary
    • 177_Nth Highest Salary
    • 178_Rank Scores
    • 180_Consecutive Numbers
    • 181_Employees Earning More Than Their Managers
    • 182_Duplicate Emails
    • 183_Customers Who Never Order
    • 184_Department Highest Salary
    • 185_Department Top Three Salaries
    • 196_Delete Duplicate Emails
    • 197_Rising Temperature
    • 570_Managers with at Least 5 Direct Reports
    • 578_Get Highest Answer Rate Question
    • 579_Find Cumulative Salary of an Employee
    • 584_Find Customer Referee
    • 586_Customer Placing the Largest Number of Orders
    • 595_Big Countries
    • 596_Classes More Than 5 Students
    • 597_Friend Requests I: Overall Acceptance Rate
    • 601_Human Traffic of Stadium
    • 602_Friend Requests II: Who Has the Most Friends
    • 603_Consecutive Available Seats
    • 607_Sales Person
    • 608_Tree Node
    • 610_Triangle Judgement
    • 612_Shortest Distance in a Plane
    • 613_Shortest Distance in a Line
    • 619_Biggest Single Number
    • 620_Not Boring Movies
    • 626_Exchange Seats
    • 627_Swap Salary
  • Facebook 面经题
    • spam filter
    • marketplace
    • instagram
    • session
    • message confirmation
Powered by GitBook
On this page
  • CASE syntax
  • IF Syntax
  • LOOP Syntax
  • WHILE Syntax
  • REPEAT Syntax
  • RETURN Syntax
  • ITERATE Syntax
  • LEAVE Syntax

Was this helpful?

  1. SQL

Control Flow Statements

CASE syntax

When we need to react differently in different conditions, we use CASE syntax. Here are two typical usages.

If we only need to test equality:

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END

If we want to test other type of conditions:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END

Here is a example for the second usage:

SELECT OrderID, Quantity,
    CASE
        WHEN Quantity > 30 THEN "The quantity is greater than 30"
        WHEN Quantity = 30 THEN "The quantity is 30"
        ELSE "The quantity is something else"
    END
FROM OrderDetails;

CASE WHEN is usually used in SELECT statement, but it's possible to be used in other statements. Here is a example:

# order the customers by City, if City is NULL, then order by Country
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
    WHEN City IS NULL THEN Country
    ELSE City
END);

IF Syntax

IF statement works similar to CASE WHEN. In practice, we use CASE WHEN more often.

IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END;

LOOP Syntax

Neglecting to include a loop-termination statement results in an infinite loop.

[begin_label:] LOOP
    statement_list
END LOOP [end_label]

Example:

CREATE PROCEDURE doiterate(p1 INT)
BEGIN
  label1: LOOP
    SET p1 = p1 + 1;
    IF p1 < 10 THEN
      ITERATE label1;
    END IF;
    LEAVE label1;
  END LOOP label1;
  SET @x = p1;
END;

WHILE Syntax

[begin_label:] WHILE search_condition DO
    statement_list
END WHILE [end_label]

Example:

CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;
  WHILE v1 > 0 DO
    ...
    SET v1 = v1 - 1;
  END WHILE;
END;

REPEAT Syntax

[begin_label:] REPEAT
    statement_list
UNTIL search_condition
END REPEAT [end_label]

Example:

mysql> delimiter //

mysql> CREATE PROCEDURE dorepeat(p1 INT)
    -> BEGIN
    ->   SET @x = 0;
    ->   REPEAT
    ->     SET @x = @x + 1;
    ->   UNTIL @x > p1 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> CALL dorepeat(1000)//
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @x//
+------+
| @x   |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)

RETURN Syntax

RETURN expr

ITERATE Syntax

ITERATE label

LEAVE Syntax

LEAVE label
PreviousCombine Queries: UNIONNextIF function

Last updated 5 years ago

Was this helpful?

implements a simple loop construct, enabling repeated execution of the statement list, which consists of one or more statements, each terminated by a semicolon (;) statement delimiter. The statements within the loop are repeated until the loop is terminated. Usually, this is accomplished with astatement. Within a stored function,can also be used, which exits the function entirely.

Astatement can be labeled.

The statement list within astatement is repeated as long as thesearch_condition_expression is true.statement_list_consists of one or more SQL statements, each terminated by a semicolon (;) statement delimiter.

Astatement can be labeled.

The statement list within astatement is repeated until thesearch_condition_expression is true. Thus, aalways enters the loop at least once.statement_list_consists of one or more statements, each terminated by a semicolon (;) statement delimiter.

Astatement can be labeled.

Thestatement terminates execution of a stored function and returns the value_expr_to the function caller. There must be at least onestatement in a stored function. There may be more than one if the function has multiple exit points.

This statement is not used in stored procedures, triggers, or events. Thestatement can be used to exit a stored program of those types.

can appear only within,, andstatements.means “start the loop again.”

This statement is used to exit the flow control construct that has the given label. If the label is for the outermost stored program block,exits the program.

can be used withinor loop constructs (,,).

LOOP
LEAVE
RETURN
LOOP
WHILE
WHILE
REPEAT
REPEAT
REPEAT
RETURN
RETURN
LEAVE
ITERATE
LOOP
REPEAT
WHILE
ITERATE
LEAVE
LEAVE
BEGIN ... END
LOOP
REPEAT
WHILE