Binds a compute column (column of results from a COMPUTE clause) to a program variable.
RETCODE dbaltbind (
PDBPROCESS dbproc,
INT computeid,
INT column,
INT vartype,
DBINT varlen,
LPCBYTE varaddr );
The dbaltbind function supports a wide range of type conversions, so the binding’s data type can be different from the type returned by the SQL query. For instance, a SQLMONEY result can be bound to a DBFLT8 program variable, using FLT8BIND, and the appropriate data conversion happens automatically.
For more information about a list of the data conversions provided by DB-Library, see dbwillconvert. For more information about a list of the type definitions used by DB-Library, see DB-Library for C Data types. The following table lists the legal vartypes recognized by dbaltbind and the program variable and SQL Server data types that each refers to.
vartype | Program variable data type | SQL Server data type |
---|---|---|
CHARBIND | DBCHAR | SQLCHAR |
STRINGBIND | DBCHAR | SQLCHAR |
NTBSTRINGBIND | DBCHAR | SQLCHAR |
VARYCHARBIND | DBVARYCHAR | SQLCHAR |
BINARYBIND | DBBINARY | SQLBINARY |
VARYBINBIND | DBVARYBIN | SQLBINARY |
TINYBIND | DBTINYINT | SQLINT1 |
SMALLBIND | DBSMALLINT | SQLINT2 |
INTBIND | DBINT | SQLINT4 |
FLT8BIND | DBFLT8 | SQLFLT8 |
BITBIND | DBBIT | SQLBIT |
DATETIMEBIND | DBDATETIME | SQLDATETIME |
MONEYBIND | DBMONEY | SQLMONEY |
SMALLMONEYBIND | DBMONEY4 | SQLMONEY4 |
SMALLDATETIBIND | DBDATETIM4 | SQLDATETIM4 |
FLT4BIND | DBFLT4 | SQLFLT4 |
DECIMALBIND | DBDECIMAL | SQLDECIMAL |
NUMERICBIND | DBNUMERIC | SQLNUMERIC |
SRCDECIMALBIND | DBDECIMAL | SQLDECIMAL |
SRCNUMERICBIND | DBNUMERIC | SQLNUMERIC |
Because SQLTEXT and SQLIMAGE data are never returned through a compute row (a row of results generated by a COMPUTE clause), these data types are not included in the preceding table. The SQL Server data type is listed for your reference. The vartype you specify does not necessarily have to correspond to a particular SQL Server data type because, as mentioned earlier, dbaltbind converts SQL Server data into the specified vartype.
The following table lists the four representations for character data. They differ according to whether the data is padded with blanks or is null-terminated:
vartype | Program data type | Padding | Terminator |
---|---|---|---|
CHARBIND | DBCHAR | blanks | none |
STRINGBIND | DBCHAR | blanks | \0 |
NTBSTRINGBIND | DBCHAR | none | \0 |
VARYCHARBIND | DBVARYCHAR | none | none |
Note that “\0” is the null terminator character. Similarly, binary data can be stored in two different ways:
vartype | Program data type | Padding |
---|---|---|
BINARYBIND | DBBINARY | nulls |
VARYBINBIND | DBVARBINARY | none |
When a column of integer data is summed or averaged, SQL Server always returns a four-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 and that the vartype of the binding is INTBIND.
When the source column specified by the column parameter has a type of SQLDECIMAL or SQLNUMERIC, you can keep the same precision and scale in your bound C variable by using SRCDECIMALBIND or SRCNUMERICBIND.
When binding using DECIMALBIND or NUMERICBIND, the varaddr parameter must be a pointer to a DBNUMERIC or DBDECIMAL C variable, respectively, with the precision and scale fields of the structure already set to the desired values. You can use DEFAULTPRECISION to specify a default precision and DEFAULTSCALE to specify a default scale.
SUCCEED or FAIL. The dbaltbind function returns FAIL if vartype isn’t compatible with the SQL Server data type being returned, or if varaddr is NULL.
This function directs DB-Library to copy compute column data returned by SQL Server into a program variable. When each new row containing computed data is read by dbnextrow or dbgetrow, the data from the designated column in that compute row is copied into the program variable with the address varaddr. There must be a separate dbaltbind call for each compute column to be copied. It is not necessary to bind every compute column to a program variable.
SQL Server can return two types of rows: regular rows containing data from columns designated by a SELECT statement’s select list, and compute rows resulting from the COMPUTE clause. The dbaltbind function binds data from compute rows. Use dbbind for binding data from regular rows.
The calls to dbaltbind must be made after a call to dbresults and before the first call to dbnextrow.
Using dbaltbind causes some overhead because it always copies the row data into the designated program variable. To avoid this copying, the returned data can be accessed more directly with dbadlen and dbadata.
Because null values can be returned from SQL Server, there is a set of default values, one for each data type, that is substituted when binding null values. You can explicitly set your own values to be substituted for the default null values with the dbsetnull function. (For more information about a list of the default substitution values, see dbsetnull.)
This example shows the typical sequence of calls:
DBCHAR name[20];
DBINT namecount;
// Read the query into the command buffer.
dbcmd(dbproc, "SELECT name FROM employee COMPUTE COUNT(name)");
// Send the query to SQL Server.
dbsqlexec(dbproc);
// Get ready to process the results of the query.
dbresults(dbproc);
// Bind the regular row data - name.
dbbind(dbproc, 1, STRINGBIND, (DBINT) 0, name);
// Bind the compute column data - count of name.
dbaltbind(dbproc, 1, 1, INTBIND, (DBINT) 0, (BYTE *) &namecount);
// Now process each row.
while (dbnextrow(dbproc) != NO_MORE_ROWS)
{
//C-code to print or process row data
}
DB-Library for C Data Types | dbgetrow |
dbadata | dbnextrow |
dbadlen | dbresults |
dbanullbind | dbsetnull |
dbbind | dbwillconvert |
dbconvert |