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

SQL External Routine Programming

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

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;