This section describes how to retrieve LONG values from the database.
The procedures are different depending on whether you are using static or dynamic SQL.
Declare a host variable of type DECL_LONGVARCHAR, DECL_LONGNVARCHAR, or DECL_LONGBINARY, as appropriate. The array_len member is filled in automatically.
Retrieve the data using FETCH, GET DATA, or EXECUTE INTO. SQL Anywhere sets the following information:
indicator variable Negative if the value is NULL, 0 if there is no truncation, otherwise the positive untruncated length in bytes up to a maximum of 32767.
stored_len The number of bytes stored in the array. Always less than or equal to array_len and untrunc_len.
untrunc_len The number of bytes that would be stored in the array if the value was not truncated. Always greater than or equal to stored_len. If truncation occurs, this value is larger than array_len.
Set the sqltype field to DT_LONGVARCHAR, DT_LONGNVARCHAR, or DT_LONGBINARY as appropriate.
Set the sqldata field to point to the LONGVARCHAR, LONGNVARCHAR, or LONGBINARY host variable structure.
You can use the LONGVARCHARSIZE(n), LONGNVARCHARSIZE(n), or LONGBINARYSIZE(n) macro to determine the total number of bytes to allocate to hold n bytes of data in the array field.
Set the array_len field of the host variable structure to the number of bytes allocated for the array field.
Retrieve the data using FETCH, GET DATA, or EXECUTE INTO. SQL Anywhere sets the following information:
* sqlind This sqlda field is negative if the value is NULL, 0 if there is no truncation, and is the positive untruncated length in bytes up to a maximum of 32767.
stored_len The number of bytes stored in the array. Always less than or equal to array_len and untrunc_len.
untrunc_len The number of bytes that would be stored in the array if the value was not truncated. Always greater than or equal to stored_len. If truncation occurs, this value is larger than array_len.
The following code fragment illustrates the mechanics of retrieving LONG VARCHAR data using dynamic embedded SQL. It is not intended to be a practical application:
#define DATA_LEN 128000 void get_test_var() { LONGVARCHAR *longptr; SQLDA *sqlda; SQLVAR *sqlvar; sqlda = alloc_sqlda( 1 ); longptr = (LONGVARCHAR *)malloc( LONGVARCHARSIZE( DATA_LEN ) ); if( sqlda == NULL || longptr == NULL ) { fatal_error( "Allocation failed" ); } // init longptr for receiving data longptr->array_len = DATA_LEN; // init sqlda for receiving data // (sqllen is unused with DT_LONG types) sqlda->sqld = 1; // using 1 sqlvar sqlvar = &sqlda->sqlvar[0]; sqlvar->sqltype = DT_LONGVARCHAR; sqlvar->sqldata = longptr; printf( "fetching test_var\n" ); EXEC SQL PREPARE select_stmt FROM 'SELECT test_var'; EXEC SQL EXECUTE select_stmt INTO DESCRIPTOR sqlda; EXEC SQL DROP STATEMENT select_stmt; printf( "stored_len: %d, untrunc_len: %d, " "1st char: %c, last char: %c\n", longptr->stored_len, longptr->untrunc_len, longptr->array[0], longptr->array[DATA_LEN - 1] ); free_sqlda( sqlda ); free( longptr ); } |
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |