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 expressionsThe 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. STORESStores are places where you can store data for later user. The @STORE can be used in a macro command for example: @STORE1 = 2; ExpressionAn 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. IF (1=1 AND 2=2) ---- IF (1<1 OR 2=2) IFThe if command allows for conditional execution of macro commands. IF (expression) 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: IF (3 > 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 (3 > 1) 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. ELSEThe 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. IF(@STORE1 > @STORE2) ELSEIFThe 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: IF(@STORE1 > @STORE2) WHILEThe while command creates a loop in the macro. The basic form of a while command is: WHILE(expression) The meaning of a while statement is simple. @STORE1 = 1; IN ClauseA special expression type is the IN clause. IF ('X' IN 'X,Y,Z')
@STORE1 = SQL(SELECT 'X,Y,Z'); IF ('X' IN @STORE1) This example will not work. @STORE1 = SQL(SELECT 'X,Y,Z'); This is not possible as the IN clause cannot be combined with other Boolean expressions. TRY/CATCHCan be used to handle error during macro execution.
TRY BEGIN (statement) END CATCH BEGIN (statement) END
You can get the error using the syntax: @LASTEXCEPTION
Example: Note: If you use TRY you also need to have a CATCH block (and vice versa)
Other notesIf 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 Example: @STORE1 = 10;
|