XML 列から XML データを取り出す例: 以下の例は、さまざまな C データ・タイプを使用して XML 列からアプリケーション変数に XML データを取り出す方法を示します。図 1. XML 列から XML データを取り出す例
/* Variables for output XML data */
SQLCHAR HVBINARY[32768];
SQLCHAR HVCHAR[32768];
SQLDBCHAR HVDBCHAR[32768];
SQLWCHAR HVWCHAR[32768];
/* Variables for output XML data lengths */
SQLINTEGER LEN_HVBINARY;
SQLINTEGER LEN_HVCHAR;
SQLINTEGER LEN_HVDBCHAR;
SQLINTEGER LEN_HVWCHAR;
/* SQL statement buffer */
SQLCHAR sqlstmt[250];
/* Return code for ODBC calls */
SQLRETURN rc = SQL_SUCCESS;
/* Prepare an SELECT statement for retrieving */
/* data from XML columns. */
strcpy((char *)sqlstmt,
"SELECT XMLCOL1, XMLCOL2, XMLCOL3, XMLCOL4 ");
strcat((char *)sqlstmt,
"FROM MYTABLE");
/* Bind data for first XML column as SQL_C_BINARY. */
/* This data will be retrieved as internally */
/* encoded, in the UTF-8 encoding scheme. */
rc = SQLBindCol(hstmt, 1, SQL_C_BINARY, HVBINARY, sizeof(HVBINARY), &LEN_HVBINARY);
/* Bind data for second XML column as */
/* SQL_C_CHAR. This data will be retrieved as */
/* internally encoded, in the application encoding */
/* scheme. */
rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, HVCHAR, sizeof(HVCHAR), &LEN_HVCHAR);
/* Bind data for third XML column as SQL_C_DBCHAR. */
/* This data will be retrieved as internally */
/* encoded, in the application encoding scheme. */
rc = SQLBindCol(hstmt, 3, SQL_C_DBCHAR, HVDBCHAR, sizeof(HVDBCHAR), &LEN_HVDBCHAR);
/* Bind data for fourth XML column as SQL_C_WCHAR. */
/* This data will be retrieved as internally */
/* encoded, in the UCS-2 encoding scheme. */
rc = SQLBindCol(hstmt, 4, SQL_C_WCHAR, HVWCHAR, sizeof(HVWCHAR), &LEN_HVWCHAR);
/* Execute the SELECT statement and fetch a row */
/* from the result set */
rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS);
rc = SQLFetch(hstmt);
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;
/* Prepare select statement */
strcpy((char *)sqlstmt, "select * from mytable");
rc = SQLPrepare(hstmt, sqlstmt, SQL_NTS);
/* Bind column data as SQL_C_BINARYXML */
rc = SQLBindCol(hstmt, 1, SQL_C_BINARYXML, sizeof(HV1BINARYXML), &LEN_HV1BINARYXML);
/* Execute select */
rc = SQLExecute(hstmt);
/* Fetch result set column as binary XML */
rc = SQLFetch(hstmt);