SQL Scripting

You can employ powerful procedural logic using SQL/PSM standard-based scripting syntax. Any SQL script consists of and starts with a compound statement block (BEGIN ... END). A compound statement starts with a section to declare local variables, cursors, user-defined conditions, and condition handlers, which are used to catch exceptions. This is followed by the compound statement body, which consists of:

Passing data between the invoker and the compound statement

There are two ways to pass data to and from a SQL script:

Variable and cursor scoping

Variables declared within a compound statement can be referenced in any expression within a compound statement. Spark resolves identifiers from the innermost scope outward, following the rules described in Name Resolution. You can use the optional compound statement labels to disambiguate duplicate variable names.

Cursors declared within a compound statement can be referenced in OPEN, FETCH, and CLOSE statements within that compound statement. Like variables, you can use optional compound statement labels to disambiguate duplicate cursor names in nested scopes.

Condition handling

SQL Scripting supports condition handlers, which are used to intercept and process exceptions to either EXIT the compound statement or CONTINUE execution.

Condition handlers can be defined to handle three distinct classes of conditions:

The following are used to decide which condition handler applies to an exception. This condition handler is called the most appropriate handler:

The outcome of a condition handler is as follows:

The following is a list of supported control flow statements:

The following is a list of cursor statements: