在 SQL 中,没有名为 “FOR” 的循环语法结构
DECLARE @counter INT = 0;
WHILE @counter < 5
BEGIN
PRINT 'Counter value: ' + CAST(@counter AS VARCHAR);
SET @counter = @counter + 1;
END;
DECLARE @employee_id INT, @employee_name NVARCHAR(50);
DECLARE employee_cursor CURSOR FOR
SELECT employee_id, employee_name FROM employees;
OPEN employee_cursor;
FETCH NEXT FROM employee_cursor INTO @employee_id, @employee_name;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Employee ID: ' + CAST(@employee_id AS VARCHAR) + ', Employee Name: ' + @employee_name;
FETCH NEXT FROM employee_cursor INTO @employee_id, @employee_name;
END;
CLOSE employee_cursor;
DEALLOCATE employee_cursor;
请注意,不同的数据库管理系统(如 MySQL、PostgreSQL、SQL Server 等)可能具有不同的语法和功能支持。因此,在实际应用中,请根据您正在使用的数据库系统查阅相关文档。