SnowConvert AI - Oracle - CREATE FUNCTION¶
Oracle Create Function to Snowflake Snow Scripting
Description¶
Note
Some parts in the output code are omitted for clarity reasons.
A stored function (also called a user function or user-defined function) is a set of PL/SQL statements you can call by name. Stored functions are very similar to procedures, except that a function returns a value to the environment in which it is called. User functions can be used as part of a SQL expression.
A call specification declares a Java method or a third-generation language (3GL) routine so that it can be called from PL/SQL. You can also use the
CALLSQL statement to call such a method or routine. The call specification tells Oracle Database which Java method, or which named function in which shared library, to invoke when a call is made. It also tells the database what type conversions to make for the arguments and return value. Oracle SQL Language Reference Create Function.
Oracle Syntax¶
For more information, see the Oracle CREATE FUNCTION documentation.
Oracle Create Function Syntax¶
Snowflake Syntax¶
Snowflake allows 3 different languages in their user-defined functions:
SQL
JavaScript
Java
For now, SnowConvert AI will support only SQL and JavaScript as target languages.
For more information, see the Snowflake UDF overview.
SQL¶
Note
SQL user-defined functions only support one query as their body. They can read from the database but are not allowed to write to or modify it (Scalar SQL UDFs).
JavaScript¶
Note
JavaScript user-defined functions allow multiple statements in their bodies but cannot perform queries to the database. (Scalar JavaScript UDFs).
Sample Source Patterns¶
Sample auxiliary data¶
Note
This code was executed for a better understanding of the examples:
Oracle¶
Snowflake¶
Known Issues¶
No issues were found.
Cursor for a return variable¶
Note
Some parts in the output code are omitted for clarity reasons.
This pattern defines a function in Oracle PL/SQL that uses a cursor to fetch a single value and return it.
Components:
Function Declaration:
CREATE FUNCTION functionName(parameters) RETURN returnTypeDeclares the function with input parameters and the return type.
Variable Declarations:
Declares variables, including the return variable.
Cursor Declaration:
CURSOR cursorName IS SELECT singleColumn FROM ... WHERE ... [AND col1 = localVar1];Defines a cursor to select a single column from a table with optional filtering conditions.
BEGIN-END Block:
Variables assignment.
Opens the cursor.
Fetch the result into the return variable.
Closes the cursor.
Returns the fetched value.
In this case, the variables are transformed into a common table expression (CTE). As well as the query within the cursor to which, in addition, the FETCH FIRST 1 ROW ONLY clause is added to simulate the FETCH CURSOR behavior.
RETURN statement is transformed to the final select.
Queries¶
Oracle¶
Snowflake¶
Result¶
FUNC1() |
|---|
2004-05-03. |
Oracle¶
Snowflake¶
Result¶
FUNC1() |
|---|
2004-05-03. |
Known Issues¶
No issues were found.
Related EWIs¶
SSC-FDM-OR0042: Date Type Transformed To Timestamp Has A Different Behavior.
SSC-EWI-0073: Pending Functional Equivalence Review.
Cursor with IF statement¶
Note
Some parts in the output code are omitted for clarity reasons.
This pattern defines a function that conditionally uses a cursor to fetch and return a value based on an IF statement.
Components:
Function Declaration:
CREATE FUNCTION functionName(parameters) RETURN returnTypeDeclares the function with input parameters and the return type.
Cursor Declaration:
CURSOR cursorName IS SELECT singleColumn FROM ... WHERE ... [AND col1 = localVar1];Defines a cursor to select a single column from a table with optional filtering conditions.
Variable Declaration:
Declares variables, including the return variable.
BEGIN-END Block with IF Statement:
Variables assignment.
Check if a condition is true.
If true, opens the cursor, fetches the result into the return variable, closes the cursor, and returns the fetched value. (The cursor can also be opened in the
ELSEblock and must meet the same conditions)The
ELSEBlock is optional, if it exists, it should only contain a single statement that can be an assignment or aRETURNstatement.
The variables are transformed into a common table expression (CTE). As well as the query within the cursor to which, in addition, the FETCH FIRST 1 ROW ONLY clause is added to simulate the FETCH CURSOR behavior.
IF/ELSE statement can be handled using the CASE EXPRESSION inside the select allowing conditionals inside the queries. RETURN statement is transformed to the final select..
Queries¶
Oracle¶
Snowflake¶
Result¶
FUNC2(0) |
|---|
NULL |
FUNC2(1) |
|---|
33 |
Oracle¶
Snowflake¶
Result¶
FUNC2(0) |
|---|
33 |
FUNC2(1) |
|---|
2 |
Oracle¶
Snowflake¶
Result¶
FUNC2(0) |
|---|
0 |
FUNC2(1) |
|---|
33 |
Known Issues¶
No issues were found.
Related EWIs¶
No EWIs related.
Multiple IF statement¶
This pattern defines a function that uses conditional statements over local variables.
Components:
Function Declaration:
CREATE FUNCTION functionName(parameters) RETURN returnTypeDeclares the function with input parameters and the return type.
Variable Declaration:
Declares variables, including the return variable.
BEGIN-END Block with IF Statement:
Check if a condition is true.
Each case is used to assign a value over the same variable.
Conversion:¶
DECLARE SECTION : variables with default expression are moved to a common table expression.
IF/ELSE statement can be handled using the CASE EXPRESSION inside the select allowing conditionals inside the queries.
RETURN statement is transformed to the final select.
Oracle¶
Snowflake¶
Oracle¶
Snowflake¶
Oracle¶
Snowflake¶
Oracle¶
Snowflake¶
Known Issues¶
No issues were found.
Related EWIs¶
SSC-FDM-OR0042: Date Type Transformed To Timestamp Has A Different Behavior.
SSC-EWI-0073: Pending Functional Equivalence Review.
SSC-EWI-OR0036: Types resolution issues, the arithmetic operation may not behave correctly between string and date.
Snowflake Script UDF (SCALAR)¶
Translation reference for Oracle User Defined Functions to Snowflake Scripting UDFs
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
SnowConvert now supports translating Oracle PL/SQL User Defined Functions directly to Snowflake Scripting UDFs (SnowScript UDFs) when they meet specific criteria.
Snowflake Scripting UDFs are user-defined functions written using Snowflake’s procedural language syntax (Snowscript) within a SQL UDF body. They support variables, loops, conditional logic, and exception handling without requiring database access.
When Functions Become SnowScript UDFs¶
SnowConvert analyzes each Oracle function and automatically determines the appropriate Snowflake target. A function becomes a SnowScript UDF when it contains only procedural logic without data access operations.
Sample Source Patterns¶
Simple Calculation Function¶
A basic function that performs calculations without querying data.
Oracle¶
Result¶
CALCULATETAX(1000, 15) |
|---|
150 |
Snowflake (SnowScript UDF)¶
Result¶
CALCULATETAX(1000, 15) |
|---|
150 |
Function with IF/ELSIF/ELSE Logic¶
Functions using conditional statements for business logic.
Oracle¶
Result¶
GETSHIPPINGCOST(75, 25) |
|---|
30 |
Snowflake (SnowScript UDF)¶
Result¶
GETSHIPPINGCOST(75, 25) |
|---|
30 |
Function with FOR Loop¶
Functions using loops for iterative calculations.
Oracle¶
Result¶
CALCULATECOMPOUNDINTEREST(1000, 5, 3) |
|---|
1157.63 |
Snowflake (SnowScript UDF)¶
Result¶
CALCULATECOMPOUNDINTEREST(1000, 5, 3) |
|---|
1157.63 |
CASE and DECODE Logic¶
Functions using CASE expressions and DECODE for categorization.
Oracle¶
Result¶
GETCUSTOMERTIER(3000, 6) |
|---|
GOLD |
Snowflake (SnowScript UDF)¶
Result¶
GETCUSTOMERTIER(3000, 6) |
|---|
GOLD |
Select Into variable assignment¶
Functions using simple select into for variable assignment.
Oracle¶
Result¶
CALCULATEPRICE(100, 3) |
|---|
285 |
Snowflake (SnowScript UDF)¶
Result¶
CALCULATEPRICE(100, 3) |
|---|
285 |
Known Issues¶
Warning
SnowConvert AI will not translate UDFs containing the following elements into SnowScripting UDFs, as these features are unsupported in SnowScripting UDFs:
Access database tables
Use cursors
Call other UDFs
Contain aggregate or window functions
Perform DML operations (INSERT/UPDATE/DELETE)
Return result sets
Related EWIs¶
SSC-EWI-0067: UDF was transformed to Snowflake procedure, calling procedures inside a query is not supported.
SSC-EWI-0068: User defined function was transformed to a Snowflake procedure.
SSC-EWI-0073: Pending Functional Equivalence Review.
SSC-FDM-OR0042: Date Type Transformed To Timestamp Has A Different Behavior.