Example: Calling a Stored Procedure from an External Stored Procedure - Analytics Database - Teradata Vantage

SQL External Routine Programming

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2023-07-11
dita:mapPath
iiv1628111441820.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
B035-1147
lifecycle
latest
Product Category
Teradata Vantage™

Consider a stored procedure that has the following definition:

CREATE PROCEDURE addRegion (IN region VARCHAR(64),
                            OUT region_count INTEGER)
  BEGIN
    INSERT INTO regionTable(:region);
    SELECT COUNT(*) INTO :region_count FROM regionTable;
  END;

The following code calls the addRegion stored procedure from the external stored procedure xsp_getregion:

#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>

void xsp_getregion ( VARCHAR_LATIN *region,
                     int           *region_isnull,
                     char          sqlstate[6],
                     SQL_TEXT      extname[129],
                     SQL_TEXT      specific_name[129],
                     SQL_TEXT      error_message[257])
{

  char   tmp_string[64];
  void  *argv[2];
  int    ind[2];
  parm_t dtype[2];
  INTEGER regionCount;   /* OUT argument from addRegion */

  /* Set the return indicator value for the external stored procedure*/
  *region_isnull = 0;

  if (strlen((const char *)region) > 4)
  {
    /* Strip off the first four characters */
    strcpy(tmp_string, (char *)region);
    strcpy((char *)region, &tmp_string[4]);
    /* Set the pointers to the stored procedure arguments */
    argv[0] = region;         /* IN */
    argv[1] = &regionCount;   /* OUT */

    /* Set the indicator for the IN argument */
    ind[0] = 0;

    memset(dtype, 2, sizeof(parm_t)*2);

    /* Data type for the VARCHAR IN argument */
    dtype[0].datatype = VARCHAR_DT;
    dtype[0].direction = IN_PM;
    dtype[0].charset = LATIN_CT;
    dtype[0].size.length = strlen((const char *)region);

    /* Data type for the INTEGER OUT argument */
    dtype[1].datatype = INTEGER_DT;
    dtype[1].direction = OUT_PM;

    FNC_CallSP((SQL_TEXT *)"addRegion", 2, argv, ind, dtype, sqlstate);

    if (strcmp(sqlstate, "00000") != 0)
    {
      strcpy((char *)error_message, "Bad call to addRegion");
      return;
    }
  }
  else
  {
    strcpy(sqlstate, "U0001");
    strcpy((char *)error_message, "Region string too short");
    return;
  }
}

Here is the CREATE PROCEDURE statement that installs the xsp_getregion external stored procedure on the server:

CREATE PROCEDURE GetRegionXSP
  (INOUT region VARCHAR(64))
LANGUAGE C
NO SQL
EXTERNAL NAME 'CS!getregion!xspsrc/getregion.c!F!xsp_getregion'
PARAMETER STYLE TD_GENERAL;