16.20 - TD_ANYTYPE Data Type - Teradata Vantage NewSQL Engine

Teradata Vantage™ Data Types and Literals

prodname
Teradata Database
Teradata Vantage NewSQL Engine
vrm_release
16.20
category
Programming Reference
featnum
B035-1143-162K

Purpose

An input or result parameter data type that is used in UDFs, UDMs, and external stored procedures, and that can accept any system-defined data type or user-defined type (UDT). The parameter attributes and return type are determined at execution time.

Syntax



parameter_name
The name of an input or result parameter declared in a UDF, UDM, or external stored procedure.

ANSI Compliance

TD_ANYTYPE is a Teradata extension to the ANSI SQL standard.

Usage Notes

The TD_ANYTYPE data type is useful in:
  • Declaring routines (functions, methods, external stored procedures) that support parameters with differing precisions and character parameters with varying character sets.
  • Avoiding precision loss of decimal parameters due to truncation when the arguments do not match with the parameters in the function signature.
  • Reducing the number of overloaded routines needed to support routines that have constant parameter counts but differing parameter data types.
  • Declaring routines whose return data type is determined at runtime.

Rules

You can only specify TD_ANYTYPE as a data type for:
  • Input parameters in scalar, aggregate and table functions written in C, C++, or Java.
  • Result parameters in scalar and aggregate functions written in C, C++, or Java.
  • IN, INOUT, or OUT parameters in external stored procedures written in C, C++, or Java.
  • Input and output parameters in UDMs written in C or C++.

Restrictions

The TD_ANYTYPE type is not supported as a parameter type for:
  • Stored procedures
  • SQL UDFs
  • UDMs written in Java
TD_ANYTYPE cannot be used:
  • As the return type in table functions
  • To represent UDT parameters in Java routines

Declaring the Return Type of a TD_ANYTYPE Result Parameter

When invoking a UDF or UDM that is defined with a TD_ANYTYPE result parameter, you can use the RETURNS data type or RETURNS STYLE column expression clause to specify the desired return type. The column expression can be any valid table or view column reference, and the return data type is determined based on the type of the column.

For example:

SELECT (routine_name  (parameter_list) RETURNS  data_type);
SELECT (routine_name  (parameter_list) RETURNS STYLE  TableName.ColumnName);

The above queries invoke routine_name using the specified parameters and declare the return type to be data_type or the data type of the column, TableName.ColumnName.

You must enclose the UDF or UDM invocation in parenthesis if you use the RETURNS or RETURNS STYLE clause.

When invoking an external stored procedure that is defined with a TD_ANYTYPE OUT parameter, you can specify the RETURNS data typeor RETURNS STYLE column expression clause along with the OUT parameter in the CALL statement to indicate the desired return type of the OUT parameter.

For example:

CALL  procedure_name(parameter1,  out_parameter  RETURNS  data_type);
CALL  procedure_name(parameter1,  out_parameter  RETURNS STYLE  TableName.ColumnName);

The RETURNS or RETURNS STYLE clause is not mandatory as long as the routine also includes a TD_ANYTYPE input parameter. If you do not specify a RETURNS or RETURNS STYLE clause, then the data type of the first TD_ANYTYPE input argument is used to determine the return type of the TD_ANYTYPE result or OUT parameter. For character types, if the character set is not specified as part of the data type, then the default character set is used.

Note that the RETURNS and RETURNS STYLE clauses are only used to set the return type for a TD_ANYTYPE OUT parameter in external stored procedures. The data type of a TD_ANYTYPE INOUT parameter is determined by the data type of the corresponding input argument.

Example: TD_ANYTYPE Data Type

Consider the following function definition:

CREATE FUNCTION ascii (string_expr TD_ANYTYPE)
   RETURNS TD_ANYTYPE
   LANGUAGE C
   NO SQL
   SPECIFIC ascii
   EXTERNAL NAME 'CS!ascii!UDFs/ascii.c'
   PARAMETER STYLE SQL;
This function returns the ASCII numeric value of the first character of the input string. It accepts input strings with the following data types:
  • CHARACTER
  • VARCHAR
  • CLOB
  • UDT with a CHARACTER, VARCHAR, or CLOB attribute.
  • ARRAY with an element type of CHARACTER or VARCHAR.

The function accepts the character set associated with the input string. If no character set is explicitly specified, the default character set is used.

The return type supported by this function is BYTEINT, SMALLINT, or INTEGER. To specify the desired return type for the function, use the RETURNS data type or RETURNS STYLE column expression clause when invoking the function.

The following is a sample query that invokes this function:

SELECT (ascii('hello') RETURNS INTEGER);

The output returned by the query is:

ascii('hello')
--------------
           104

For more details about this function, including the corresponding C code example for the function, see Teradata Vantage™ SQL External Routine Programming , B035-1147 .

Related Topics

FOR information on … SEE …
declaring TD_ANYTYPE input and result parameters in UDFs “CREATE FUNCTION” in Teradata Vantage™ SQL Data Definition Language Syntax and Examples, B035-1144, External Form and Table Form.
declaring TD_ANYTYPE input and output parameters in methods “CREATE METHOD” in Teradata Vantage™ SQL Data Definition Language Syntax and Examples, B035-1144.
declaring TD_ANYTYPE IN, INOUT, or OUT parameters in external stored procedures “CREATE PROCEDURE (External Form)” in Teradata Vantage™ SQL Data Definition Language Syntax and Examples, B035-1144.
writing routines that use TD_ANYTYPE input and result parameters Teradata Vantage™ SQL External Routine Programming , B035-1147 .