Scratchpad as C or C++ function parameter
The scratchpad structure, used for storing UDF values between invocations for each UDF input value, is supported in C and C++ routines through the use of the sqludf_scrat structure.
The sqludf_scrat
C structure is
defined in the include file sqludf.h.
To reference the sqludf_scrat structure, include the sqludf.h header file at the top of the file containing the C or C++ function implementation, and use the SQLUDF_TRAIL_ARGS_ALL macro within the signature of the routine implementation.
#ifdef __cplusplus
extern "C"
#endif
void SQL_API_FN ScratchpadScUDF(SQLUDF_INTEGER *outCounter,
SQLUDF_SMALLINT *counterNullInd,
SQLUDF_TRAIL_ARGS_ALL)
{
struct scalar_scratchpad_data *pScratData;
/* SQLUDF_CALLT and SQLUDF_SCRAT are */
/* parts of SQLUDF_TRAIL_ARGS_ALL */
pScratData = (struct scalar_scratchpad_data *)SQLUDF_SCRAT->data;
switch (SQLUDF_CALLT)
{
case SQLUDF_FIRST_CALL:
pScratData->counter = 1;
break;
case SQLUDF_NORMAL_CALL:
pScratData->counter = pScratData->counter + 1;
break;
case SQLUDF_FINAL_CALL:
break;
}
*outCounter = pScratData->counter;
*counterNullInd = 0;
} /* ScratchpadScUDF */
The SQLUDF_TRAIL_ARGS_ALL macro expands to define other parameter values including one called SQLUDF_SCRAT that defines a buffer parameter to be used as a scratchpad. When the scalar function is invoked for a set of values, for each time the scalar function is invoked, the buffer is passed as a parameter to the function. The buffer can be used to be accessed
The SQLUDF_TRAIL_ARGS_ALL macro value also defines another parameter SQLUDF_CALLT. This parameter is used to indicate a call type value. Call type values can be used to identify if a function is being invoked for the first time for a set of values, the last time, or at a time in the middle of the processing.