dbgetoff

Checks for the existence of Transact-SQL statements in the command buffer.

Syntax

INT dbgetoff (
PDBPROCESS
dbproc,
DBUSMALLINT
offtype,
INT
startfrom );

where

dbproc
Is the DBPROCESS structure that is the handle for a particular workstation/ SQL Server process. It contains all the information that DB-Library uses to manage communications and data between the workstation and SQL Server.
offtype
Is the type of offset you want to find. The types (defined in the header file SQLFRONT.H) are: OFF_SELECT, OFF_FROM, OFF_ORDER, OFF_COMPUTE, OFF_TABLE, OFF_PROCEDURE, OFF_STATEMENT, OFF_PARAM, and OFF_EXEC

For details, see DB-Library Options.

startfrom
Is the point in the buffer from which to start looking. The command buffer begins at 0.

Returns

The character offset into the command buffer for the specified offset. If the offset is not found, -1 is returned.

Remarks

If the DBOFFSET option has been set (see DB-Library Options), dbgetoff can check for the location of certain Transact-SQL statements in the command buffer.

Examples

In this example, assume that the program doesn't know the contents of the command buffer but needs to know where the SQL keyword SELECT appears:

int        select_offset[10];
int        last_offset;
int        i;

// Set the offset option. 
dbsetopt(dbproc, DBOFFSET, "select");

dbsqlexec(dbproc);        // Execute the option on the server  while(dbresults(dbproc)!=NO_MORE_RESULTS); // Read returned results  
// Assume the command buffer contains the following SELECTs: 
dbcmd(dbproc, "select x = 100 select y = 5");

// Send the query to SQL Server. 
dbsqlexec(dbproc);

// Get all the offsets to the SELECT keyword. 
for (i = 0, last_offset = 0; last_offset != -1; i+)
if ((last_offset = dbgetoff(dbproc, OFF_SELECT, last_offset))!= -1)
    select_offset[i] = last_offset+;

    dbresults(dbproc);

In this example, select_offset[0] = 0 and select_offset[1] = 15.

The function dbgetoff does not recognize SELECT statements in a subquery. So, if the command buffer contains the following, the second SELECT statement goes unrecognized:

select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = "business")

See Also

dbcmd, dbgetchar, dbsetopt, dbstrcpy, dbstrlen; Bulk-Copy Functions, and DB-Library Options