github.com/mithrandie/csvq@v1.18.1/docs/_posts/2006-01-02-control-flow.md (about)

     1  ---
     2  layout: default
     3  title: Control Flow - Reference Manual - csvq
     4  category: reference
     5  ---
     6  
     7  # Control Flow
     8  
     9  * [IF](#if)
    10  * [CASE](#case)
    11  * [WHILE](#while_loop)
    12  * [WHILE IN](#while_in_loop)
    13  * [CONTINUE](#continue)
    14  * [BREAK](#break)
    15  * [EXIT](#exit)
    16  * [TRIGGER ERROR](#trigger_error)
    17  
    18  _IF_ statements and _WHILE_ statements create local scopes.
    19  [Variables]({{ '/reference/variable.html' | relative_url }}), [cursors]({{ '/reference/cursor.html' | relative_url }}), [temporary tables]({{ '/reference/temporary-table.html' | relative_url }}), and [functions]({{ '/reference/user-defined-function.html' | relative_url }}) declared in statement blocks can be refered only within the blocks. 
    20  
    21  ## IF
    22  {: #if}
    23  
    24  ```sql
    25  IF condition THEN statements
    26    [ELSEIF condition THEN statements ...]
    27    [ELSE statements]
    28  END IF;
    29  ```
    30  
    31  _condition_
    32  : [value]({{ '/reference/value.html' | relative_url }})
    33  
    34  _statements_
    35  : [Statements]({{ '/reference/statement.html' | relative_url }})
    36  
    37  IF statement executes the first _statements_ that _condition_ is TRUE.
    38  If no condition is TRUE, the _statements_ of the ELSE expression are executed.
    39  
    40  ## CASE
    41  {: #case}
    42  
    43  ### Case with condition
    44  
    45  ```sql
    46  CASE
    47    WHEN condition THEN statements
    48    [WHEN condition THEN statements]
    49    [ELSE statements]
    50  END CASE;
    51  ```
    52  
    53  _condition_
    54  : [value]({{ '/reference/value.html' | relative_url }})
    55  
    56  _statements_
    57  : [Statements]({{ '/reference/statement.html' | relative_url }})
    58  
    59  Execute _statements_ of the first WHEN expression that _condition_ is TRUE.
    60  If no condition is TRUE, then execute _statements_ of the ELSE expression.
    61  
    62  
    63  ### Case with comparison
    64  
    65  ```sql
    66  CASE value
    67    WHEN comparison_value THEN statements
    68    [WHEN comparison_value THEN statements]
    69    [ELSE statements]
    70  END CASE;
    71  ```
    72  
    73  _value_
    74  : [value]({{ '/reference/value.html' | relative_url }})
    75  
    76  _comparison_value_
    77  : [value]({{ '/reference/value.html' | relative_url }})
    78  
    79  _statements_
    80  : [Statements]({{ '/reference/statement.html' | relative_url }})
    81  
    82  Execute _statements_ of the first WHEN expression that _comparison_value_ is equal to _value_.
    83  If no _comparison_value_ is match, then execute _statements_ of the ELSE expression.
    84  
    85  
    86  ## WHILE
    87  {: #while_loop}
    88  
    89  ```sql
    90  WHILE condition
    91  DO
    92    statements
    93  END WHILE;
    94  ```
    95  
    96  _condition_
    97  : [value]({{ '/reference/value.html' | relative_url }})
    98  
    99  _statements_
   100  : [Statements]({{ '/reference/statement.html' | relative_url }})
   101  
   102  A While statement evaluate _condition_, then if condition is TRUE, executes _statements_. 
   103  The While statement iterates it while _condition_ is TRUE.
   104  
   105  ## WHILE IN
   106  {: #while_in_loop}
   107  ```sql
   108  WHILE [DECLARE|VAR] variable [, variable ...] IN cursor_name
   109  DO
   110    statements
   111  END WHILE;
   112  ```
   113  
   114  _variable_
   115  : [Variable]({{ '/reference/variable.html' | relative_url }})
   116  
   117  _cursor_name_
   118  : [identifier]({{ '/reference/statement.html#parsing' | relative_url }})
   119  
   120  _statements_
   121  : [Statements]({{ '/reference/statement.html' | relative_url }})
   122  
   123  A While In statement fetch the data from the [cursor]({{ '/reference/cursor.html' | relative_url }}) into variables, then execute _statements_.
   124  The While In statement iterates it until the _cursor_name_ pointer reaches the last record in the referring view.
   125  
   126  If DECLARE or VAR keyword is specified, then variables are declared in the child scope. 
   127  Otherwise, variables in the current scope is used to fetch.
   128  
   129  ## CONTINUE
   130  {: #continue}
   131  
   132  ```sql
   133  CONTINUE;
   134  ```
   135  
   136  A Continue statement stops statements execution in loop, then jumps to the next iteration.
   137  
   138  ## BREAK
   139  {: #break}
   140  
   141  ```sql
   142  BREAK;
   143  ```
   144  
   145  A Break statement stops statements execution in loop, then exit from current loop.
   146  
   147  ## EXIT
   148  {: #exit}
   149  
   150  ```sql
   151  EXIT [exit_code];
   152  ```
   153  
   154  _exit_code_
   155  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   156  
   157    0 is the default.
   158  
   159  Exit statement stops statements execution, then terminates the executing procedure without commit.
   160  
   161  ## TRIGGER ERROR
   162  {: #trigger_error}
   163  
   164  ```sql
   165  TRIGGER ERROR [exit_code] [error_message];
   166  ```
   167  
   168  _exit_code_
   169  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   170  
   171    64 is the default.
   172  
   173  _error_message_
   174  : [string]({{ '/reference/value.html#string' | relative_url }})
   175  
   176  A trigger error statement stops statements execution, then terminates the executing procedure with an error.