17.10 - Example: Table Function with Fixed Result Row Specification - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

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

Here is an example of how to declare a table function:

/*****  C source file name: store_data.c  *****/
   
#define  SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
 
void get_store_data( INTEGER  *filetoread,    /* input argument  */
                     INTEGER  *storeno,       /* output argument */
                     INTEGER  *itemno,        /* output argument */
                     INTEGER  quantsold,      /* output argument */
                     int      *filetoreadIsNull,
                     int      *storenoIsNull,
                     int      *itemnoIsNull,
                     int      *quantsoldIsNull;
                     char     sqlstate[6],
                     SQL_TEXT extname[129],
                     SQL_TEXT specific_name[129],
                     SQL_TEXT error_message[257] )
{
     ...
}

The corresponding CREATE FUNCTION statement that installs the table function on the server looks like this:

CREATE FUNCTION getStoreData
  (FileToRead INTEGER)
RETURNS TABLE
   (StoreNo   INTEGER
   ,ItemNo    INTEGER
   ,QuantSold INTEGER)
LANGUAGE C
NO SQL
EXTERNAL NAME 'CS!getstoredata!udfsrc/store_data.c!F!get_store_data'
PARAMETER STYLE SQL;

Here is an example of an INSERT ... SELECT statement that invokes the table function in the FROM clause:

INSERT INTO Sales_Table
SELECT *
FROM TABLE (getStoreData(9005)) AS tf;