Conditions
Adjust your SQL code dynamically according to conditions
Introduction
The logic blocks supports conditional execution through the use of if and else, allowing dynamic SQL code to be executed only when certain conditions are met.
Syntax
The syntax of these conditions is as follows:
Simple Conditions
Includes the code only if the condition is true, without an alternative.
Conditions with Else
Allow two content alternatives. If the condition is not true, the content defined after {:else}
will be included.
Multiple Conditions
Allow unlimited content alternatives. It evaluates conditions cascading until one is met. If none is met, it returns the content defined after {:else}
.
Expressions
Expressions are crucial for determining the logic within if and else constructs. They can evaluate variables, constants, or any combination of the two using a variety of operators.
Values
Values can be inside expressions:
- Query parameters: Values passed to the query. For example,
param('user_id')
could be a query parameter representing a user’s ID. Check out Query Parameters for more information. - Variables/Parameters: Variables can be defined in the query and used in expressions. For example,
id
could be a variable representing a user’s ID. Check out Parameters for more information.
Operations
Complex operations can be performed within expressions:
-
Arithmetic Operations: Basic arithmetic operations can be performed within expressions.
-
String Operations: String operations can be performed within expressions.
Comparisons
Expressions are compared with operators, which include the following options:
- Equality and Inequality:
==
: Equal to!=
: Not equal to
- Greater Than or Less Than:
>
: Greater than<
: Less than<=
: Less than or equal to>=
: Greater than or equal to
- Multiple conditions:
&&
: Logical AND to concatenate multiple conditions in the same expression. They must all be true to continue.||
: Logical OR to concatenate multiple conditions in the same expression. At least one of them must be true to continue.