16.20 - Example: FNC_SetXML - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
16.20
Release Date
April 2020
Content Type
Programming Reference
Publication ID
B035-1147-162K
Language
English (United States)

The following UDF takes an XML parameter as input, retrieves the XML value and sets the return XML value with it. It assumes the XML size is small enough to be read using a single read.

CREATE FUNCTION xmlUDF3(P1 XML)
RETURNS XML
NO SQL
PARAMETER STYLE SQL
DETERMINISTIC
LANGUAGE C
EXTERNAL NAME 'CS!xmlUDF3!xmlUDF3.c';

void xmlUDF3(XML_HANDLE* xml_handle,
             XML_HANDLE* return_handle,
             int* indicator_thisXML,
             int* indicator_returnValue,
             char    sqlstate[6],
             SQL_TEXT              extname[129],
             SQL_TEXT              specific_name[129],
             SQL_TEXT              error_message[257] )
{
     FNC_XMLSize_t xmlSize;
     byte* xmlBuffer;
     FNC_XMLSize_t xmlBufferSize;
     int numLobs;

    /* Get the XML Size */
      FNC_GetXMLInfo(*xml_handle, &xmlSize, &numLobs); 

     if(numLobs ==1 )
     {  /* Return null if a LOB is passed in */
        *indicator_returnValue = -1;
         return;
     }
       
    /* Read the XML value. “+ 2” for the Unicode null termination character. */
     xmlBuffer = (byte*)FNC_Malloc(xmlSize + 2);
     xmlBufferSize = xmlSize + 2;
      FNC_GetXML(*xml_handle,xmlBuffer, xmlBufferSize, &xmlSize); 
                                                                                                                           
     /* Set the return value */
      FNC_SetXML(*return_handle, xmlBuffer, xmlSize); 
    *indicator_returnValue = 0;
     FNC_free(xmlBuffer);
}