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

Teradata Database SQL Data Definition Language Syntax and Examples

prodname
Teradata Database
vrm_release
15.10
created_date
December 2015
category
Programming Reference
featnum
B035-1144-151K

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 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 SQL Functions, Operators, Expressions, and Predicates, B035-1145 and SQL Data Manipulation Language, B035-1146 for information about the NEW VARIANT_TYPE constructor expression.