The statement is the smallest executable unit in the syntax. A SQL procedure is a sequence of statements. Statements are separated by semicolon (;). There are many kinds of statements, such as declare statement, expression statement, assignment statement, if-else statement, and return statement.
The declare statement is used to declare variables.
VariableType VariableName
Example: String sql;
This statement declares a local String type variable called sql.
global VariableType VariableName
Example: global integer i;
This statement declares a global Integer type variable called i.
The expression statement is used to calculate the value of an expression.
The assignment statement is used to calculate the expression on the right side of assignment operator and assign the value to the variable on the left side.
Syntax: Variable = Expression
The type of value of expression must be compatible with the type of the variable.
Example: total = amount * price
In this example, where the total, amount and price are declared variable. The statement calculate the "amount * price" and assign the value to "total".
The if-else statement provides conditional control of an action. Normally, the statements are executed one by one sequentially. The if-else statement enables alternative actions that depend on the evaluation result of the expression.
Syntax:
|
Where, the expression must be a logical expression and have a Boolean value. The if clause checks the condition presented by the logical expression. If the condition is true, the statement following the if will be executed. Otherwise, the statement following the else will be executed.
Example:
|
In this example, if the score is greater than or equal to 60, the result is pass, otherwise the result is fail.
The return statement is used to stop the execution of procedure and return a value if necessary. The return statement is the last statement executed.
Syntax: return or return expression;
The expression will be calculated before the procedure returns.
Example: return results;
The for statement provides a compact way to iterate over a range of values.
Syntax:
for (initialization; termination; increment) {
statement
}
Or
for (initialization; termination; increment) statement
The initialization is an expression that initializes the loop - it's executed once at the beginning of the loop. The termination expression determines when to terminate the loop. This expression is evaluated at the top of each iteration of the loop. When the expression evaluates to false, the loop terminates. Finally, increment is an expression that gets invoked after each iteration through the loop. All these components are optional.
Example:
|
Both examples will return 100 as the result.
The while statement is used to continually execute a block of statements while a condition remains true.
Syntax:
while (expression) do {
statement
}
Or
while(expression) do statement
First, the while statement evaluates expression, which must return a Boolean value. If the expression returns true, then the while statement executes the statement(s) associated with it. The while statement continues testing the expression and executing its block until the expression returns false.
Example:
|
Both examples will return 100 as the result.
Note: JReport Designer provides another statement that is similar to the while statement - the do-while statement.
syntax:
do { statement(s) } while (expression);
Or
do statement while(expression)
Instead of evaluating the expression at the top of the loop, do-while evaluates the expression at the bottom. Thus the statements associated with a do-while are executed at least once.
Example:
|
Both examples will return 100 as the result.
The select statement is usually used in the case that the value of a single variable may determine one of a number of different choices.
A select statement is given a variable and compares its value to all cases in the switch; if there is a case that matches the value, all statements in the matching case are executed. If none match, and a default is given, all statements following the default keyword are executed.
Syntax:
select (variable name){
case expression_1: statement
case expression_2: statement
...
default: statement
};
The expression_1 and expression_2 should be variables or constants. The statement in each should be a single statement or multi-statement (compound statement). When multi-statement is used, they must be enclosed by {}.
Notes:
Example:
|