17.10 - Example: FNC_GetXMLResultClob and FNC_SetXMLClob - 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)

The following function takes an input CLOB value and returns an XML value back to the user.

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

void xmlUDF4(LOB_LOCATOR* input_clob,
             XML_HANDLE  *xml_handle,
             int*       indicator_inputCLOB,
             int*       indicator_returnXML,
             char       sqlstate[6],
             SQL_TEXT   extname[129],
              SQL_TEXT   specific_name[129],
              SQL_TEXT   error_message[257] )

{
     LOB_RESULT_LOCATOR xmlClob;
     BYTE buffer[100000];
     LOB_CONTEXT_ID id;
     FNC_LobLength_t actlen;
     int trunc_err = 0;
     int numLobs;
     FNC_XMLSize_t xmlSize;
    
     /* Get the XML Size */
      FNC_GetXMLInfo(*xml_handle, &xmlSize, &numLobs); 

     if(numLobs ==1 )
     {
      /* Read XML return lob locator */ 
       FNC_GetXMLResultClob(*xml_handle,&xmlClob); 

     /* Use LOB FNC calls to read the input CLOB into the XML clob */                                                                                                                           
      FNC_LobOpen(*input_clob, &id, 0, 0);
      while( FNC_LobRead(id, buffer, 100000, &actlen) == 0 && !trunc_err)
        trunc_err = FNC_LobAppend(*xmlClob, buffer, actlen, &actlen);
       FNC_LobClose(id);

      /* Set the return XML value */
       FNC_SetXMLClob(*xml_handle,xmlClob); 
      *indicator_returnXML = 0;
     }
 }