Conditional Macro commands
|Previous Next | Direct link to this topic|
The conditional commands are commands that allow you to create if/elseif/else/while statements in the Macro-system.
Introduction to conditional commands and expressions
The most basic forms of expressions in the macro system are stores. When working with stores think of them as expressions with a value. When you type "@STORE1 = 5", you're assigning '5' into @STORE1. '5', obviously, has the value 5, or in other words '5' is an expression with the value of 5.
After this assignment, you'd expect @STORE1’s value to be 5 as well, so if you wrote @STORE2 = @STORE1, you'd expect it to behave just as if you wrote @STORE2 = 5. In other words, @STORE1 is an expression with the value of 5 as well. This is also how the macro system handles it.
A very common type of expressions are comparison expressions. These expressions evaluate to either false or true. Macro supports > (bigger than), >= (bigger than or equal to), = (equal), != (not equal), < (smaller than) and <= (smaller than or equal to). These expressions are used inside conditional execution, such as if statements.
Stores are places where you can store data for later user.
The @STORE can be used in a macro command for example:
An expression is a set of statements that evaluate to either true or false. An expression can be used in the IF/ELSE/ELSEIF and WHILE statements to execute specific logic when the expression is true.
The if command allows for conditional execution of macro commands.
As described in the section above about expressions, expression is evaluated to either true or false. If the expression evaluates to true, macro will execute the statement, and if it evaluates to false - it'll ignore it.
The following example would create a messagebox with “3 is bigger than 1” if 3 is bigger than 1:
Often you'd want to have more than one statement to be executed conditionally. You can group several commands into a group. For example, this code would display “3 is bigger than 1” if 3 is bigger than 1 and would then assign the value 1 to @STORE1
IF commands can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of the macro.
The else command can be used if you want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to false.
The elseif command is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display STORE1 is bigger than STORE2, STORE1 equal to STORE2 or STORE1 is smaller than STORE2:
The while command creates a loop in the macro. The basic form of a while command is:
The meaning of a while statement is simple.
A special expression type is the IN clause.
This example will not work.
This is not possible as the IN clause cannot be combined with other Boolean expressions.
Can be used to handle error during macro execution.
You can get the error using the syntax: @LASTEXCEPTION
Note: If you use TRY you also need to have a CATCH block (and vice versa)
If working with numbers that you divide from @stores and you are using fractions please be aware that it might be needed to specify using SQL that you are truly working with decimal numbers
@STORE1 = 10;