WHILE statement
Applies to ❌ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
One of the most commonly used loop types is the WHILE
statement, which in its procedural form works just like a Java while
statement with slightly different syntax.
Procedural dialects may or may not use the LOOP
keyword to delimite the loop body, just as with the previous LOOP statement. The jOOQ API always uses it for DSL design reasons. For example, combining the WHILE
loop with the variable assignment statement:
-- PL/SQL syntax WHILE i <= 10 LOOP INSERT INTO t (col) VALUES (i); END LOOP;
// All dialects Variable<Integer> i = var("i", INTEGER); while_(i.le(10)).loop( insertInto(T).columns(T.COL).values(i) )
Notice that while
is a reserved keyword in the Java language, so the jOOQ API cannot use it as a method name. We've suffixed such conflicts with an underscore: while_()
.
Dialect support
This example using jOOQ:
while_(i.le(10)).loop(deleteFrom(BOOK).where(BOOK.ID.eq(i)))
Translates to the following dialect specific expressions:
-- AURORA_POSTGRES, EXASOL, INFORMIX, ORACLE, POSTGRES, YUGABYTEDB WHILE i <= 10 LOOP DELETE FROM BOOK WHERE BOOK.ID = i; END LOOP -- BIGQUERY WHILE i <= 10 DO DELETE FROM BOOK WHERE ( BOOK.ID = i AND TRUE ); END WHILE -- DB2, HANA, HSQLDB, MARIADB, MYSQL WHILE i <= 10 DO DELETE FROM BOOK WHERE BOOK.ID = i; END WHILE -- FIREBIRD WHILE (:i <= 10) DO BEGIN DELETE FROM BOOK WHERE BOOK.ID = :i; END -- H2 while (i <= 10) { try (PreparedStatement s = c.prepareStatement( "DELETE FROM BOOK\n" + "WHERE BOOK.ID = ?" )) { s.setObject(1, i); s.execute(); } } -- SQLDATAWAREHOUSE, SQLSERVER WHILE @i <= 10 BEGIN DELETE FROM BOOK WHERE BOOK.ID = @i; END -- ACCESS, ASE, AURORA_MYSQL, COCKROACHDB, DERBY, DUCKDB, MEMSQL, REDSHIFT, SNOWFLAKE, SQLITE, SYBASE, TERADATA, TRINO, -- VERTICA /* UNSUPPORTED */
(These are currently generated with jOOQ 3.19, see #10141), or translate your own on our website
Feedback
Do you have any feedback about this page? We'd love to hear it!