mysql存储过程中使用while循环

mysql存储过程中使用while循环

while循环
CREATE DEFINER=`root`@`%` PROCEDURE `DisplayEmployeesWhile`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE emp_name VARCHAR(100);
DECLARE emp_position VARCHAR(100);
DECLARE emp_id INT DEFAULT 0;

-- 获取员工的总数
SELECT COUNT(*) INTO @total_employees FROM employees;
select concat("总条数:",@total_employees);
-- 循环显示员工信息
WHILE emp_id < @total_employees DO
-- 获取当前员工信息
select concat("第",emp_id + 1,"次循环");
SELECT name, position INTO emp_name, emp_position
FROM employees
LIMIT emp_id, 1;

-- 显示员工信息
SELECT CONCAT('Name: ', emp_name, ', Position: ', emp_position) AS EmployeeInfo;

-- 增加员工计数
SET emp_id = emp_id + 1;
END WHILE;
END

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: