Return a pointer to the data for a compute column.
BYTE *dbadata(dbproc, computeid, colnum) DBPROCESS *dbproc; int computeid; int colnum;
A pointer to the DBPROCESS structure that provides the connection for a particular front-end/server process. It contains all the information that DB-Library uses to manage communications and data between the front end and server.
The ID that identifies the particular compute row of interest. A SQL select statement may have multiple compute clauses, each of which returns a separate compute row. The computeid corresponding to the first compute clause in a select is 1. The computeid is returned by dbnextrow or dbgetrow.
The number of the column of interest. The first column returned is number 1. Note that the order in which compute columns are returned is determined by the order of the corresponding columns in the select list, not by the order in which the compute columns were originally specified. For example, in the following query the result of “sum(price)” is referenced by giving colnum a value of 1, not 2:
select price, advance from titles
compute sum(advance), sum(price)
The relative order of compute columns in the select list, rather than their absolute position, determines the value of colnum. For instance, given the following variation of the previous select:
select title_id, price, advance from titles
compute sum(advance), sum(price)
the colnum for “sum(price)” still has a value of 1 and not 2, because the “title_id” column in the select list is not a compute column and therefore is ignored when determining the compute column’s number.
A BYTE pointer to the data for a particular column in a particular compute. Be sure to cast this pointer into the proper type. A BYTE pointer to NULL is returned if there is no such column or compute or if the data has a null value.
DB-Library allocates and frees the data space that the BYTE pointer points to. Do not overwrite this space.
After each call to dbnextrow, you can use this routine to return a pointer to the data for a particular column in a compute row. The data is not null-terminated. You can use dbadlen to get the length of the data.
When a column of integer data is summed or averaged, the server always returns a 4-byte integer, regardless of the size of the column. Therefore, be sure that the variable that is to contain the result from such a compute is declared as DBINT.
Here is a short program fragment which illustrates the use of dbadata:
DBPROCESS *dbproc;
int rowinfo;
DBINT sum;
/*
** First, put the commands into the command
** buffer
*/
dbcmd(dbproc, "select db_name(dbid), dbid, size
from sysusages");
dbcmd(dbproc, " order by dbid");
dbcmd(dbproc, " compute sum(size) by dbid");
/*
** Send the commands to Adaptive Server Enterprise and start
** execution
*/
dbsqlexec(dbproc);
/* Process the command */
dbresults(dbproc);
/* Examine the results of the compute clause */
while((rowinfo = dbnextrow(dbproc)) !=
NO_MORE_ROWS)
{
if (rowinfo == REG_ROW)
printf("regular row returned.\n");
else
{
/*
** This row is the result of a compute
** clause, and "rowinfo" is the computeid
** of this compute clause.
*/
sum = *(DBINT *)(dbadata(dbproc, rowinfo,
1));
printf("sum = %ld\n", sum);
}
}
The function dbaltbind automatically binds compute data to your program variables. It does a copy of the data, but is often easier to use than dbadata. Furthermore, it includes a convenient type conversion capability. By means of this capability, the application can, among other things, easily add a null terminator to a result string or convert money and datetime data to printable strings.
dbadlen, dbaltbind, dbaltlen, dbalttype, dbgetrow, dbnextrow, dbnumalts