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

SQL Data Definition Language Syntax and Examples

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
wgr1555383704548.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1144
lifecycle
previous
Product Category
Teradata Vantage™

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 the CREATE FUNCTION/REPLACE FUNCTION topic “Function Name Overloading” in the 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.