Reference Guide > TDV SQL Script > SQL Script Statement Reference > LOOP
 
LOOP
The LOOP statement is used in SQL Script for looping through the current block.
Syntax
[<label>:] LOOP
<statements>
END LOOP [<label>]
 
This sample statement loops forever. You need to use a LEAVE statement to exit it.
Remarks
The label is an optional identifier to name the block. This is for use with the LEAVE and ITERATE statements. See LEAVE and ITERATE.
If a beginning label is present, the end label is not required. If no beginning label is present, then it is illegal to have an end label. If both the beginning and end labels are present, then both must have the same identifier.
There can be zero or more statements in the <statements> area.
Example
This example pads s with padChar so that s has at least width length.
PROCEDURE padr(IN a VARCHAR, IN width INTEGER, IN padChar VARCHAR, OUT result VARCHAR)
--pad result with padChar
  SET result = s;
  L-loop:
  LOOP
  IF LENGTH(result) >= width THEN
    LEAVE L_loop;
  END IF;
  SET result = CAST(CONCAT(result, padChar) AS VARCHAR);
  END LOOP;
END