Example 1 - Advanced SQL Engine - Teradata Database

SQL Operators and User-Defined Functions

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2023-04-27
dita:mapPath
qqu1556127655717.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1210
lifecycle
previous
Product Category
Teradata Vantage™

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