In this tutorial, you will learn how to retrieve the records returned by executing an SQL statement.
We call a group of records returned by a query a result set (or recordset for those who are familiar with VB). The general steps in retrieving a result set are as follows:
When you're done with the result set, you need to destroy it by calling SQLCloseCursor.
Sometimes you already know whether a result set will be created by examination of the SQL statement. If the SQL statement is not of a type that returns a result set, you know that no result set will be available. However, sometimes you don't even know of what type the SQL statement is, such is the case when you let the user enters custom SQL statements. In that case, you must check whether a result set was created by calling SQLNumResultCols. This function returns the number of columns (fields) in the result set (if one exists) and has the following syntax:
SQLNumResultCols proto StatementHandle:DWORD, pNumCols:DWORD
If the value in the variable pointed to by pNumCols is 0, there is no result set.
In this regard, the concept is identical to that of binding a variable to a parameter of an SQL statement. You associate (bind) a variable to a specific column in the result set. The function in this case is SQLBindCol which has the following syntax:
SQLBindCol proto StatementHandle:DWORD,
ColumnNumber:DWORD,
TargetType:DWORD,
TargetValuePtr:DWORD,
BufferLength:DWORD,
pStrLenOrIndPtr:DWORD
Example:
.data?
buffer db 21 dup(?)
DataLength dd ? ; will be filled with the length
of the string in buffer after SQLFetch is called.
.code
.....
invoke SQLBindCol, hStmt, 1, SQL_C_CHAR, addr
buffer, 21, addr DataLength
It's quite simple. Calling SQLFetch retrieves a row from the result set into the bound variables. After SQLFetch is called, the cursor is updated. You can think of a cursor as the record pointer. It indicates what row will be returned when SQLFetch is called. For example, if the result set has 4 rows, the cursor is positioned at the first row when the result set was created. When SQLFetch is called, the cursor advances by 1 row. So if you call SQLFetch 4 times, there is no more row to fetch. The cursor is said to point to the end of file (EOF). SQLFetch has the following syntax:
SQLFetch proto StatementHandle:DWORD
This function returns SQL_NO_DATA when no more row is available.
Example:
.data?
buffer db 21 dup(?)
DataLength dd ?
.code
.....
invoke SQLBindCol, hStmt, 1, SQL_C_CHAR, addr
buffer, 21, addr DataLength
invoke SQLFetch, hStmt