PL/SQL Translation Guidelines
Oracle’s proprietary PL/SQL language allows developers to create functions / procedures / packages in which queries can be executed and retrieved data can be processed in a procedural fashion. In some cases, for example, when implementing algorithms that need to work on data structures not natively supported by standard SQL datatypes and iteratively processed, PL/SQL can be helpful.
Sometimes PL/SQL is used to group multiple dependent SQL statements and execute them sequentially without loading data from the database into PL/SQL, as a surrogate for a multi-step ETL-Job for example. That’s usually the focus of ETL-Tools.
Every time the Oracle engine needs to switch from the SQL context to PL/SQL there is an overhead involved. These context switches have a negative impact on performance. So, it is highly recommended to use SQL instead of PL/SQL whenever possible.
When migrating from Oracle to Exasol, rethink the design choices made in Oracle that may no longer be the best option for Exasol, especially when migrating PL/SQL from Oracle to Exasol.
Sometimes, due to the complexity of the dependencies, interfaces and behaviors of old structures must be supported in a first stage of the migration, so that they can re-factored later.
This article helps you translate PL/SQL in Oracle to Lua in Exasol. Lua is a scripting language that is tightly integrated in Exasol. User Defined Functions (UDFs) in Exasol, which can be used in SQL statements, can be written in Lua, Python, Java and R. Within UDFs you can use the capabilities of the programming language of your choice, and even integrate external libraries, to write functions that can be used in SQL.
Scripts on the other hand can only be written in Lua and allow you, for example, to send queries to Exasol, fetch the results, transform the data, and write it back to Exasol again.
Scripts in Exasol, just as PL/SQL in Oracle, are not designed to process masses of data, but rather to design and control process flow and group processes into one executable script. Therefore, for mass processing data in the Database always use SQL.
Oracle provides built-in procedures and packages, and some of them might be in use in your Oracle environment. The DBMS_SCHEDULER
package automatically executes processes depending on a defined time, event or dependency (to another process). Exasol does not have a built-in scheduler but an external scheduler, like cron can be used instead. For more information, see Scheduling Database Jobs.
For more information about:
- Scripts, see Scripting and CREATE SCRIPT.
- UDFs, see UDF Scripts.
- Lua, see Lua Documentation.
Datatypes
While in PL/SQL variables and their datatypes must be pre-defined in the declare block of the code, variables in Lua can be created on the spot without datatype definition.
Oracle PL/SQL |
Exasol Lua |
Comments |
---|---|---|
|
|
|
Numbers |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Datetime |
||
|
Not supported |
For
more information on |
|
Not supported |
|
|
Not supported |
|
|
Not supported |
|
Character |
||
|
|
|
|
|
|
|
|
|
|
|
|
Data Structures |
|
|
|
|
Indexing tables with Integers |
|
|
The equivalent would be a table, but the fetched data from a query is loaded into a read-only userdata structure first.
|
|
|
Variables
The following table shows you examples of how variables are translated.
Description | Oracle PL/SQL | Exasol |
---|---|---|
Shows the definition of a variable and assignment of a value. Within double square brackets, the whole string is interpreted as it is, including newlines. |
|
|
Shows the definition of a constant. Constants are supported by Lua 5.4, which is used in Exasol 7.1. |
|
|
Shows a variable with a referenced data type. In the example the variable To check if a variable contains a value that fits into a referenced data type, the data type of the reference must be retrieved, and the type of the value must be checked. |
|
Not supported |
Shows how a single value form an SQL query can be assigned to a variable with a referenced data type. The query function in Exasol’s Lua returns a user data object, that can contain multiple rows and columns. The first index of the returned object defines the row, the second index the column. Instead of an index, the column names / identifiers can be used too. If the columns are not defined as case sensitive, they are accessed in uppercase column names. It is recommended to use standard SQL identifiers. To send queries to Exasol from a script, the functions query or pquery must be used. |
|
|
Shows the data types of a whole row are referenced by the variable and loaded via a select into. |
|
|
Control Structures
The following table shows you examples of how control structures are translated.
Description | Oracle PL/SQL | Exasol |
---|---|---|
Shows a basic loop that does 6 iterations (0 - 5), with a break/exit condition within the loop. The |
||
Shows three different variants of a
For more information, see Numeric FOR and Generic FOR in Lua Documentation. |
|
|
Shows For more information, see IF Then ELSE in Lua Documentation. |
||
Shows a For more information, see WHILE in Lua Documentation. The query function accepts a second parameter after the query, for string replacement. For object names in the database, the replacement string needs double colons, while other replacements just need one colon. For example, |
||
The GOTO command should be avoided and rewritten with other control structures and functions for readability and maintenance reasons, even though its supported. The following example shows how it can be translated without using GOTO. For more information on |
Cursor
The following table shows you examples of how cursors are translated.
Exceptions
Errors and exceptions in Lua can be handled easily by encapsulating the piece of code in a functional and calling that function through the pcall
function. For more information, see Lua - Error Handling and Exceptions.
Errors can also occur when interacting with Exasol over SQL and for that there is a protected query method, pquery()
. For more information, see Exasol – Database Interaction.