XML データを XML 列に挿入する例: 以下の例は、さまざまな C データ・タイプと SQL データ・タイプを使用して XML データを XML 列に挿入する方法を示します。図 1. XML データを XML 列に挿入する例
/* Variables for input XML data */
SQLCHAR HVCHAR[32768];
SQLWCHAR HVWCHAR[32768];
/* Variables for input XML data lengths */
SQLINTEGER LEN_HVCHAR;
SQLINTEGER LEN_HVWCHAR;
/* SQL statement buffer */
SQLCHAR sqlstmt[250];
/* Return code for ODBC calls */
SQLRETURN rc = SQL_SUCCESS;
/* Prepare an INSERT statement for inserting */
/* data into an XML column. The input parameter */
/* type is SQL_XML, so DB2 does an implicit */
/* XMLPARSE. */
strcpy((char *)sqlstmt,
"INSERT INTO MYTABLE(XMLCOL) VALUES(?)");
/* Bind input XML data with the SQL_C_CHAR type, */
/* to an SQL_XML SQL type. */
/* The data is assumed to be externally encoded, */
/* in the application encoding scheme. */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_XML,
0, 0, HVCHAR, sizeof(HVCHAR), &LEN_HVCHAR);
/* Execute the INSERT statement */
rc = SQLExecute(hstmt);
/* Bind input XML data with the SQL_C_WCHAR type, */
/* to an SQL_XML SQL type. */
/* The data is assumed to be externally encoded, */
/* in the UCS-2 encoding scheme. */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_XML,
0, 0, HVWCHAR, sizeof(HVWCHAR0, &LEN_HVWCHAR);
/* Execute the INSERT statement */
rc = SQLExecute(hstmt);
/* Prepare an INSERT statement for inserting */
/* data into an XML column. The input parameter */
/* type is SQL_CLOB, so the application must */
/* do an explicit XMLPARSE. */
strcpy((char *)sqlstmt,
"INSERT INTO MYTABLE (XMLCOL) ");
strcat((char *)sqlstmt,
"VALUES(XMLPARSE(DOCUMENT CAST ? AS CLOB))");
/* Bind input XML data with the SQL_C_CHAR type, */
/* to an SQL_CLOB SQL type. */
/* An explicit XMLPARSE is required for inserting */
/* character data into an XML column. */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CLOB,
32768, 0, HVCHAR, sizeof(HVCHAR), &LEN_HVCHAR);
/* Execute the INSERT statement */
rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS);
バイナリー XML データを XML 列に挿入する例: 以下の例は、SQL_C_BINARYXML データ・タイプを使用して バイナリー XML データを XML 列に挿入する方法を示します。
図 2. バイナリー XML データを XML 列に挿入する例
CREATE TABLE MYTABLE ( XML_COL XML );
/* Declare variables for binary XML data */
SQLCHAR HV1BINARYXML[100];
SQLINTEGER LEN_HV1BINARYXML;
SQLCHAR sqlstmt[250];
SQLRETURN rc = SQL_SUCCESS;
/* Assume that HV1BINARYXML contains XML data in binary format
and LEN_HV1BINARYXML contains the length of data in bytes */
/* Prepare insert statement */
strcpy((char *)sqlstmt, "insert into mytable values(?)");
rc = SQLPrepare(hstmt, sqlstmt, SQL_NTS);
/* Bind XML_COL column as SQL_C_BINARYXML */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARYXML, SQL_XML,
0, 0, HV1BINARYXML, sizeof(HV1BINARYXML), &LEN_HV1BINARYXML);
/* Execute insert */
rc = SQLExecute(hstmt);