Oracle® OLAP DML Reference 10g Release 2 (10.2) Part Number B14346-03 |
|
|
View PDF |
Within an OLAP DML program, the GOTO command alters the sequence of statement execution within a program.
Syntax
GOTO label
Arguments
The name of a label elsewhere in the program constructed following the "Guidelines for Constructing a Label". Execution of the program branches to the line directly following the specified label.
Note that label, as specified in GOTO, must not be followed by a colon. However, the actual label elsewhere in the program must end with a colon.
Notes
Guidelines for Constructing a Label
When you use control structures to branch to a particular location, you must provide a label for the location in order to identify it clearly. When creating a label, follow these guidelines:
The first character in the label must be a letter, period (.
), or underscore (_
).
The remaining characters in a label can be any combination of letters, numbers, periods, or underscores.
A label must be followed immediately by a colon (:
).
Make sure that the first eight bytes in the label are unique. (Remember that, in your character set, a byte might or might not be equivalent to one character.) A label can contain up to 3999 bytes (the maximum length of a text line minus 1 byte for the colon that identifies a label). However, because only the first eight bytes of a label name are used, you can experience problems with label names greater than eight bytes when the first eight bytes are not unique.
Missing GOTO Label
When an actual label that corresponds to label does not exist elsewhere in the same program, execution stops with an error.
GOTO with IF and WHILE
A GOTO statement can be used with IF...THEN...ELSE Command and WHILE to set up conditional branching, using the following syntax.
IF boolean-expression
THEN GOTO label1
ELSE GOTO label2
However, to preserve the clarity of your programming logic, you should minimize your use of GOTO. You can often replace GOTO with one or more statements executed conditionally using FOR, IF...THEN...ELSE Command, or WHILE. You can also use a SWITCH command statement to handle different cases within the same program.
GOTO with FOR
You can use a GOTO statement in a FOR loop to branch within, or out of, the loop. This changes the sequence of statement execution, depending on where the GOTO statement and the label are positioned.
A GOTO in a FOR loop that branches to a label within the same loop makes execution continue at the label without affecting the current dimension status. Subsequent repetitions of the loop continue normally. To branch to the end of the loop, just before the DOEND statement, you should consider using a CONTINUE statement instead.
A GOTO in a FOR loop that branches to a label outside the loop terminates the effect of the FOR statement. Execution continues at the specified label and dimension status is restored to what it was before the loop. To branch to the statement immediately following the DOEND of a loop, you should consider using a BREAK statement instead.
When you use a GOTO statement outside a FOR loop to branch into the loop (that is, to a label inside the loop), an error occurs after execution passes through the rest of the loop once.
TEMPSTAT and GOTO Statements
Within a FOR loop of a program, when a DO ... DOEND phrase follows TEMPSTAT, status is restored when the DOEND, BREAK, or GOTO is encountered.
Alternatives to GOTO Statement
While GOTO makes it easy to branch within a program, frequent use of it can obscure the logic of your program, making it difficult to follow its flow. This is particularly true when you have a complex program with several labels and GOTO statements that skip over large portions of code.
To keep the logic of your programs clear, minimize your use of GOTO.
Sometimes a GOTO statement is the best programming technique, but often there are better alternatives. For example:
Instead of using GOTO statements in a FOR statement, you can often place your alternative sets of statements between DO ... DOEND statements within an IF...THEN...ELSE Command statement itself.
When each set of statements is long or you want to use them in more than one place in your program, then you might consider placing them in subprograms. Then, you can use an IF...THEN...ELSE Command statement to choose between two different programs, or use a SWITCH command statement to choose among many different programs.
"Using a FOR Statement for Looping Over Values" illustrates how the FOR command loops over values. "Using DO/DOEND in a FOR Loop" illustrates using DO ... DOEND within a FOR loop.
Examples
Example 16-2 Using GOTO with IF
This example shows a program that will produce a report for one of three areas, depending on what argument the user supplies when running the program. When the user specifies EAST
, WEST
, or CENTRAL
, execution branches to a corresponding label, and the statements following it (statement group 1, 2, or 3) are executed. When the user specifies anything else, execution branches to the argerror
label, after which statements will handle the error.
DEFINE flexrpt PROGRAM PROGRAM IF NOT INLIST('East\nWest\nCentral', UPCASE(ARG(1))) THEN GOTO argerror SWITCH &UPCASE(ARG(1)) DO CASE 'EAST': ..." (statement group 1) BREAK CASE 'WEST': ... "(statement group 2) BREAK CASE 'CENTRAL': ..." (statement group 3) BREAK DOEND argerror: ..." statements to handle error) END