17.10 - Example 1 - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Operators and User-Defined Functions

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Administration
Programming Reference
Publication ID
B035-1210-171K
Language
English (United States)

Consider the following table definition and data:

CREATE TABLE pRecords (pname CHAR(30),
                       pkey INTEGER);
SELECT * FROM pRecords;

The output from the SELECT statement is:

pname                                  pkey
------------------------------  -----------
Tom                                       6
Bob                                       5
Jane                                      4

The following is the SQL definition of a scalar UDF that calculates the factorial of an integer argument:

CREATE FUNCTION factorial (i INTEGER)
RETURNS INTEGER
SPECIFIC factorial
LANGUAGE C
NO SQL
PARAMETER STYLE TD_GENERAL
NOT DETERMINISTIC
RETURNS NULL ON NULL INPUT
EXTERNAL NAME 'ss!factorial!factorial.c!F!fact'

The following query uses the scalar UDF expression to calculate the factorial of the pkey column + 1.

SELECT pname, factorial(pkey)+1 
FROM pRecords;

The output from the SELECT statement is:

pname                           (factorial(pkey)+1)
------------------------------  -------------------
Tom                                             721
Bob                                             121
Jane                                             25