17.10 - Example of Setting the Value of a JSON Type - 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)

This example uses FNC_SetInternalValue to create a JSON instance from a string.

SQL definition:

REPLACE FUNCTION setJSONValue (a1 VARCHAR(100))
RETURNS JSON(100)
NO SQL
PARAMETER STYLE TD_GENERAL
DETERMINISTIC
LANGUAGE C
EXTERNAL NAME 'CS!setJSONValue!setJSONValue.c!F!setJSONValue';
C function definition, setJSONValue.c:
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
#include <stdio.h>

void setJSONValue (VARCHAR_LATIN *jsonstr,
                   JSON_HANDLE *json_instance,
                   char sqlstate[6])
{
   FNC_SetInternalValue((*json_instance),
                        (void*)jsonstr,
                         strlen(jsonstr));
}

Example query:

SELECT setJSONValue('{"CompanyName":"Teradata"}').JSONExtractValue('$.CompanyName');

Result:

setJSONValue('{"CompanyName":"Teradata"}').JSONEXTRACTVALUE(
------------------------------------------------------------
Teradata