在 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 等)可能具有不同的語法和功能支持。因此,在實際應用中,請根據您正在使用的數據庫系統查閱相關文檔。