17.10 - Example: Creating and Using a VARIANT_TYPE Input Parameter UDT Data Type in a UDF Definition - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Data Definition Language Syntax and Examples

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

This example creates a C aggregate UDF defined with an input parameter of type VARIANT_TYPE.

First create a new distinct data type named integer_udt.

CREATE TYPE integer_udt AS INTEGER FINAL;

Now create a new aggregate function named udf_agch002002dynudt that uses the input parameter parameter_1 with a dynamic UDT type of VARIANT_TYPE.

CREATE FUNCTION udf_agch002002dynudt (
  parameter_1 VARIANT_TYPE)  
RETURNS integer_udt CLASS AGGREGATE(4) 
LANGUAGE C  
NO SQL  
EXTERNAL NAME 'CS!udf_agch002002dynudt!udf_agch002002dynudt.c'  
PARAMETER STYLE SQL;

You can then use udf_agch002002dynudt in a SELECT request with the NEW VARIANT_TYPE constructor expression as follows:

SELECT udf_agch002002dynudt(NEW VARIANT_TYPE (tbl1.a AS a, 
      (tbl1.b + tbl1.c) AS b))
FROM Tbl1;

This SELECT request creates a dynamic UDT with two attributes named a and b.

See CREATE FUNCTION in Teradata Vantage™ - SQL Data Definition Language Detailed Topics, B035-1184 for an explanation of how writing UDFs using VARIANT_TYPE can reduce or eliminate the overhead of creating multiple UDFs to handle function name overloading.

See Teradata Vantage™ - SQL Operators and User-Defined Functions, B035-1210 and Teradata Vantage™ - SQL Data Manipulation Language, B035-1146 for information about the NEW VARIANT_TYPE constructor expression.