• 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: LOOP

December 20, 2023

MySQL Command: LOOP

MySQL is a popular open-source relational database management system that is widely used for web applications and other data-driven projects. It provides a wide range of commands and functions to manipulate and query data efficiently. One such command is the LOOP command, which allows you to iterate over a set of records and perform certain actions based on specific conditions.

Understanding the LOOP Command

The LOOP command in MySQL is used to create a loop that iterates over a set of records until a specific condition is met. It is commonly used in stored procedures and functions to perform repetitive tasks or calculations. The basic syntax of the LOOP command is as follows:

LOOP
    -- statements to be executed
END LOOP;

Within the loop, you can include any valid SQL statements, such as SELECT, INSERT, UPDATE, or DELETE, to manipulate the data. The loop continues to execute until the condition specified in the loop body evaluates to false or until an explicit EXIT statement is encountered.

Example Usage

Let’s consider an example where we have a table named “employees” with columns “id,” “name,” and “salary.” We want to increase the salary of all employees by 10% using the LOOP command. Here’s how we can achieve this:

DELIMITER $$
CREATE PROCEDURE increase_salary()
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE emp_id INT;
    DECLARE emp_salary DECIMAL(10, 2);
    
    -- Cursor to fetch employee records
    DECLARE cur CURSOR FOR SELECT id, salary FROM employees;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
    OPEN cur;
    
    read_loop: LOOP
        FETCH cur INTO emp_id, emp_salary;
        
        IF done THEN
            LEAVE read_loop;
        END IF;
        
        -- Increase salary by 10%
        UPDATE employees SET salary = emp_salary * 1.1 WHERE id = emp_id;
    END LOOP;
    
    CLOSE cur;
END$$
DELIMITER ;

In this example, we first define a stored procedure named “increase_salary” that uses the LOOP command to iterate over the employee records. We declare variables to store the employee ID and salary, and a cursor to fetch the records from the “employees” table. We also define a CONTINUE HANDLER to handle the case when no more records are found.

Inside the loop, we fetch the values of emp_id and emp_salary from the cursor and update the salary by multiplying it by 1.1 (10% increase). The loop continues until all records are processed, and then the cursor is closed.

Summary

The LOOP command in MySQL is a powerful tool for performing repetitive tasks or calculations on a set of records. It allows you to iterate over the records until a specific condition is met, making it useful in various scenarios. By using the LOOP command effectively, you can automate complex data manipulations and improve the efficiency of your MySQL queries.

If you are interested in learning more about VPS hosting and how it can benefit your business, consider exploring Server.HK. With their top-notch VPS solutions, you can enjoy reliable and high-performance hosting for your applications.

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