MySQL存储过程是一种预编译的SQL代码集合,它可以接受参数、执行逻辑并返回结果。存储过程的优势包括:
-- 创建一个简单的存储过程
DELIMITER //
CREATE PROCEDURE GetEmployeeCount(IN department_id INT, OUT employee_count INT)
BEGIN
SELECT COUNT(*) INTO employee_count FROM employees WHERE department_id = department_id;
END //
DELIMITER ;
-- 调用存储过程
CALL GetEmployeeCount(1, @employee_count);
SELECT @employee_count;
领取专属 10元无门槛券
手把手带您无忧上云