Delen via


WHILE-instructie

Van toepassing op:aangevinkt als ja Databricks SQL aangevinkt als ja Databricks Runtime 16.3 en hoger

Belangrijk

Deze functie bevindt zich in openbare preview-versie.

Herhaal de uitvoering van een lijst met instructies terwijl aan een voorwaarde wordt voldaan.

Deze instructie mag alleen worden gebruikt binnen een samengestelde instructie.

Syntaxis

[ label : ] WHILE cond DO
  { stmt ; } [...]
  END WHILE [ label ]

Parameterwaarden

  • etiket

    Een optioneel label voor de lus, dat uniek is voor alle labels voor instructies waarin de WHILE-instructie is opgenomen. Het label kan worden gebruikt om de lus te LEAVE of te ITERATE.

  • cond

    Elke expressie die resulteert in een BOOLEAN

  • stmt

    Een SQL-instructie

Voorbeelden

-- sum up all odd numbers from 1 through 10
> BEGIN
    DECLARE sum INT DEFAULT 0;
    DECLARE num INT DEFAULT 0;
    sumNumbers: WHILE num < 10 DO
      SET num = num + 1;
      IF num % 2 = 0 THEN
        ITERATE sumNumbers;
      END IF;
      SET sum = sum + num;
    END WHILE sumNumbers;
    VALUES (sum);
  END;
 25

-- Compare with the much more efficient relational computation:
> SELECT sum(num) FROM range(1, 10) AS t(num) WHERE num % 2 = 1;
 25