SQL Constructs in Routines
The following SQL constructs are specifically provided for use in the body of a routine.
Assignment Using SET
The SET statement is used to assign a value to a variable declared in a routine or an output parameter of a procedure (i.e. a parameter with mode OUT or INOUT).
Examples:SET A = 5; SET X = NULL; SET Y = 11 + A; SET D = CURRENT_DATE; SET Z = NEXT_VALUE OF Z_SEQUENCE; SET X = CASE Y WHEN 1 THEN Y ELSE 0 END;
Conditional Execution Using IF
The IF statement provides a mechanism for conditional execution of SQL statements based on the truth value of a conditional expression.
Note: If the conditional expression includes (or equals) NULL, the conditional expression evaluates to false. Testing for the NULL value must be done by using IS NULL, see the Mimer SQL Reference Manual.
A basic IF statement consists of a conditional expression followed by a list of one or more SQL statements in a THEN clause, which are executed if the conditional expression evaluates to true and, optionally, a list of one or more SQL statements in an ELSE clause which are executed if the conditional expression evaluates to false.
All of the predicates supported by Mimer SQL are allowed in the conditional expression of an IF statement - see the Mimer SQL Reference Manual.
One or more IF statements can be nested, one within the other, by using an ELSEIF clause in place of the ELSE clause in the IF statement containing another.
The IF statement does not in any sense define a local scope, it is simply a mechanism for conditionally executing a sequence of SQL statements.
Once the SQL statements to be executed have been selected, they execute in the same way as any ordinary sequence of SQL statements. This point is particularly important when considering exception condition handling behavior, see Managing Exception Conditions.
Examples:IF X > 50 THEN SET X = 50; SET Y = 1; ELSE SET Y = 0; END IF; IF Y IN (2,3,4) THEN ... ELSE ... END IF; IF X > 50 THEN SET X = 50; SET Y = 2; ELSEIF X > 25 THEN SET Y = 1; ELSE SET Y = 0; END IF; IF NOT EXISTS (SELECT * FROM TABLE_1) THEN ... ELSE ... END IF; IF X > (SELECT C1 FROM T2 WHERE ... ) THEN ... ELSE ... END IF;
Conditional Execution - the CASE Statement
The CASE statement provides another mechanism for conditional execution of SQL statements. The CASE statement comes in two forms, a "simple case" and a "searched case".
A simple case works by evaluating equality between one value expression and one or more alternatives of a second value expression. For example:DECLARE Y INTEGER; CASE Y WHEN 1 THEN ... WHEN 2 THEN ... WHEN 3 THEN ... ELSE ... END CASE;
A "searched case" works by evaluating, for truth, a number of alternative search conditions. For example:CASE WHEN EXISTS (SELECT * FROM BILL) THEN ... WHEN X > 0 OR Y = 1 THEN ... ELSE ... END CASE;
About Case Statements
For both forms of the CASE statement the following is true:
- A sequence of one or more SQL statements can follow the THEN clause for each of the conditional alternatives, in the same way as for an IF statement, even though only a single implied SQL statement is shown in the examples above.
- Each alternative sequence of SQL statements in a CASE statement is treated in the same way, with respect to the behavior of exception handlers etc., as has already been described for sequences of SQL statements in an IF statement, see Conditional Execution Using IF.
- Like the IF statement, the CASE statement simply provides a mechanism for selecting a sequence of SQL statements to execute. The CASE statement as a whole is not considered, in any sense, to be a single statement.
- The conditional part of each WHEN clause is evaluated, working from the top of the CASE statement down. The SQL statements that are actually executed are those following the THEN clause of the first WHEN condition to evaluate to true. If none of the WHEN conditions evaluate to true, the SQL statements following the CASE statement ELSE clause are executed.
The presence of an ELSE clause in the CASE statement is optional and if it is not present (and none of the WHEN conditions evaluate to true) an exception condition is raised to indicate that a case was not found for the CASE statement.
Note: If it is desired that there be no operation performed and no exception condition raised if none of the WHEN conditions evaluate to true, then an ELSE clause should be specified as an empty compound SQL statement.
Only the single selected sequence of SQL statements that follow a THEN or the ELSE is executed before the CASE statement terminates. There is no potential "fall through" to subsequent THEN sequences as is found in case statements in some other programming environments.
Note: The CASE statement is distinct from the CASE expression - see the Mimer SQL Reference Manual.
Iteration Using LOOP
The LOOP statement may be preceded by a label that can be used as an argument to LEAVE in order to terminate the loop. The LOOP statement can contain a sequence of one or more SQL statements which are executed, in order, repeatedly.
The iteration is terminated by executing the LEAVE statement, or if an exception condition is raised.
Example:L1: LOOP IF ... LEAVE L1; ... END LOOP L1;
Iteration Using WHILE
The WHILE statement may be preceded by a label that can be used as an argument to LEAVE in order to terminate the while loop. The WHILE statement can contain a sequence of one or more SQL statements which are executed, in order, repeatedly.
The WHILE statement includes a conditional expression and iteration continues as long as this expression evaluates to true. Iteration may also be terminated by executing the LEAVE statement, or if an exception condition is raised.
Example:SET I = 0; L1: WHILE I <= 10 DO ... SET I = I + 1; END WHILE L1;
Iteration Using REPEAT
The REPEAT statement may be preceded by a label that can be used as an argument to LEAVE in order to terminate the repeat loop. The REPEAT statement can contain a sequence of one or more SQL statements which are executed, in order, repeatedly.
The REPEAT statement includes an UNTIL clause, which specifies a conditional expression, and iteration continues until this expression evaluates to true. Iteration may also be terminated by executing the LEAVE statement, or if an exception condition is raised.
Example:SET I = 0; L1: REPEAT ... SET I = I + 1; UNTIL I > 10 END REPEAT L1;
Invoking Procedures - CALL
The CALL statement is used to invoke a procedure. The name of the procedure may be qualified with the name of the schema to which it belongs. A value expression or target variable must be specified for each of the procedure's parameters, see the Mimer SQL Reference Manual for the definition of a target variable.
If the procedure parameter has mode OUT or INOUT, a target variable must be specified. For procedure parameters with mode IN, a value expression may be specified.
SQL/PSM is not strongly typed, so the expression specified for each procedure parameter need not have exactly the same data type as the parameter, however the expression must be assignment-compatible with the procedure parameter for which it is supplied, see the Mimer SQL Reference Manual for a discussion of assignment and implicit data type conversions.
Examples:CALL PROC1(); CALL PROC2(X,Y); CALL IDENT1.PROC7(CURRENT_DATE, X+3, Z);
Invoking Functions - Use as a Value Expression
Functions are not invoked by calling them explicitly. A function is invoked, and it returns its value, when it is used in a procedure-control-statement or in an assignment where a value-expression would normally be used.
The name of the function may be qualified with the name of the schema to which it belongs.
For a definition of the procedure-control-statement, see the Mimer SQL Reference Manual.
The following restrictions apply:
- If the function is defined as NOT DETERMINISTIC, it must not be used in the expression that follows the CASE keyword or the WHEN keyword in the simple case form of a CASE statement, see Conditional Execution - the CASE Statement.
- If MODIFIES SQL DATA has been specified for the access-clause of the function, it must not be used in the expression following the DEFAULT keyword in a DECLARE VARIABLE statement.
Examples:IF MY_FN(X) > 70 THEN ... ELSE ... END IF; SET X = NEXT_AVAIL(TOP, LAST_ALLOC); SELECT TRANSGRESS(ARRIVE,DEPART) FROM BOOK_GUEST WHERE ... ; INSERT INTO ROOM_PRICES VALUES(CODE_MAP('LAPONIA'), ... ; SELECT * FROM ROOM_STATUS WHERE BOOKADM.STATES(STATUS) = ... ; CREATE TABLE OFFICES ( OFFICE_NAME CHAR(20), CHECK (CHECK_NAMES(OFFICE_NAME) = 0), ... ;
Comments in Routines
Any text that occurs after "--" and before end-of-line in a routine is taken to be a comment.
Example:CREATE PROCEDURE MY_PROCEDURE(Y INTEGER) -- This is a comment: Note that Y has mode IN (default) READS SQL DATA BEGIN DECLARE B INTEGER; -- Here is another comment SET B = Y + 22; -- Y is input to the procedure ... END
The following groups of SQL statement may not be used in a routine:
- Access Control statements
- Data Definition statements
- Embedded SQL Control statements
- Security Control statements
- Dynamic SQL statements
- System Administration statements.
Refer to the Mimer SQL Reference Manual for a definition of the statement groups mentioned above.
Note: Any SQL statements used in a routine must be executable, so the usual restriction on the use of SELECT versus SELECT INTO applies (only the latter being considered executable - the former may, however, be used in a conditional expression, e.g. in an IF statement or a cursor declaration).
The following restrictions apply to result set procedures:
- A COMMIT or ROLLBACK statement must not be executed in a result set procedure because it will interfere with the open cursor that will exist in the context from where the result set procedure is called.
- A function or procedure which executes a COMMIT or ROLLBACK statement must not be invoked from within a result set procedure.
- A function or procedure which has MODIFIES SQL DATA specified for its access clause must not be invoked from within a result set procedure.
Upright Database Technology AB
Voice: +46 18 780 92 00
Fax: +46 18 780 92 40