• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

MySQL Command: LEAVE

December 20, 2023

MySQL Command: LEAVE

MySQL is a popular open-source relational database management system that provides a wide range of commands to manipulate and query data. One such command is the LEAVE command, which is used to exit a loop within a stored program or a compound statement.

Understanding the LEAVE Command

The LEAVE command is primarily used in conjunction with the LOOP statement in MySQL. It allows you to exit the loop prematurely based on a specific condition. When the LEAVE command is encountered, the execution flow jumps to the end of the loop, bypassing any remaining statements within the loop.

The syntax for using the LEAVE command is as follows:

LOOP
    -- statements
    IF condition THEN
        LEAVE;
    END IF;
    -- more statements
END LOOP;

Here, the condition is evaluated, and if it evaluates to true, the LEAVE command is executed, causing the loop to terminate immediately.

Example Usage

Let’s consider an example to understand the practical usage of the LEAVE command. Suppose we have a table named “employees” with columns “id” and “name”. We want to iterate through all the employees and print their names until we find an employee with a specific ID. Once we find that employee, we want to exit the loop.

DELIMITER //
CREATE PROCEDURE print_employee_names()
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE emp_id INT;
    DECLARE emp_name VARCHAR(255);
    DECLARE cur CURSOR FOR SELECT id, name FROM employees;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
    OPEN cur;
    
    read_loop: LOOP
        FETCH cur INTO emp_id, emp_name;
        IF done THEN
            LEAVE read_loop;
        END IF;
        
        IF emp_id = 12345 THEN
            LEAVE read_loop;
        END IF;
        
        SELECT emp_name;
    END LOOP;
    
    CLOSE cur;
END //
DELIMITER ;

In this example, we define a stored procedure named “print_employee_names” that uses a cursor to fetch employee records from the “employees” table. We iterate through the cursor using a loop and check if the employee ID matches the desired ID. If it does, we use the LEAVE command to exit the loop prematurely.

Summary

The LEAVE command in MySQL is a powerful tool for controlling the flow of execution within loops. It allows you to exit a loop prematurely based on a specific condition. By using the LEAVE command effectively, you can optimize your code and improve its readability.

If you are interested in learning more about VPS hosting solutions, consider checking out Server.HK. They offer reliable and high-performance VPS hosting services tailored to meet your specific needs.

Recent Posts

  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?
  • Why No-ICP-Filing Hong Kong Hosting Is the Smart Choice for Cross-Border E-Commerce
  • Hong Kong VPS vs AWS Hong Kong Region: Cost, Latency, and Control Compared
  • Data Privacy Laws in Hong Kong: What VPS Users Need to Know
  • Hong Kong VPS Security Checklist: 10 Steps to Harden Your Server in 2026

Recent Comments

  1. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026
  2. levitra price on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  3. finasterid on Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?
  4. doxycycline hyclate 100mg on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  5. ciprofloxacin 500 mg tablet on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot