FOR

FOR is a keyword in SQL that is utilized to iterate through a set of statements for a specified number of times. It is often used in procedural language structures involving loops and conditions. However, it's worth noting that not all SQL environments support the FOR keyword, and alternatives may be used depending on the specific database management system in use.

Example

DECLARE
message VARCHAR2(1000);
BEGIN
FOR i IN 1..5 LOOP
message := message || i || ' ';
END LOOP;
DBMS_OUTPUT.PUT_LINE (message);
END;

Output

1 2 3 4 5

Explanation

The FOR loop statement provides a convenient mechanism for executing SQL statements multiple times. In the above example, a FOR loop runs from 1 to 5, each time concatenating the loop index to a message string. The message string is then output to the console after the loop has finished executing, resulting in the output 1 2 3 4 5.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.