在 SQL 中,没有名为 “Foreach” 的循环结构
DECLARE @id INT
DECLARE cur CURSOR FOR SELECT id FROM your_table
OPEN cur
FETCH NEXT FROM cur INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
-- 在这里执行你的操作,例如:
UPDATE your_table SET column = 'new_value' WHERE id = @id
FETCH NEXT FROM cur INTO @id
END
CLOSE cur
DEALLOCATE cur
DECLARE @id INT, @max_id INT
SET @id = (SELECT MIN(id) FROM your_table)
SET @max_id = (SELECT MAX(id) FROM your_table)
WHILE @id <= @max_id
BEGIN
-- 在这里执行你的操作,例如:
UPDATE your_table SET column = 'new_value' WHERE id = @id
SET @id = @id + 1
END
WITH cte AS (
SELECT id, column
FROM your_table
WHERE some_condition
UNION ALL
SELECT yt.id, yt.column
FROM your_table yt
INNER JOIN cte ON yt.id = cte.id + 1
WHERE yt.some_condition
)
UPDATE your_table
SET column = 'new_value'
FROM cte
WHERE your_table.id = cte.id
注意事项: