SQLDescribeCol returns the result descriptor — column name, type, column size, decimal digits, and nullability — for one column in the result set. This information also is available in the fields of the IRD.
An application typically calls
SQLDescribeCol after a call to
SQLPrepare and before or after the associated call to
SQLExecute. An application can also call
SQLDescribeCol after a call to
SQLExecDirect.
SQLDescribeCol retrieves the column name, type, and length generated by a SELECT statement. If the column is an expression, *
ColumnName is either an empty string or a driver-defined name.
The following example demonstrates the use of
SQLDescribeCol after a call to
SQLExecDirect:
' ****************************************************************************************
' Example of use of the ODBC raw API functions
' Demonstrates the use of the SQLDescribeCol function.
' SED_PBCC - Use the PBCC compiler
' ****************************************************************************************
#COMPILE EXE
#INCLUDE "ODBCAPI352.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL r AS INTEGER
LOCAL hEnv AS DWORD
LOCAL hDbc AS DWORD
LOCAL hStmt AS DWORD
LOCAL szInConnectionString AS ASCIIZ * 1025
LOCAL szOutConnectionString AS ASCIIZ * 1025
' Allocates the environment handle
r = SQLAllocHandle (%SQL_HANDLE_ENV, %SQL_NULL_HENV, hEnv)
IF ISFALSE SQL_SUCCEEDED(r) OR ISFALSE hEnv THEN EXIT FUNCTION
' Tells to the driver manager that is an application that uses the ODBC driver 3.x
r = SQLSetEnvAttr (hEnv, %SQL_ATTR_ODBC_VERSION, BYVAL %SQL_OV_ODBC3, %SQL_IS_INTEGER)
IF ISFALSE SQL_SUCCEEDED(r) THEN GOTO Terminate
' Allocates the connection handle
r = SQLAllocHandle (%SQL_HANDLE_DBC, hEnv, hDbc)
IF ISFALSE SQL_SUCCEEDED(r) THEN GOTO Terminate
' Connection string
szInConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=biblio.mdb;UID=;PWD=;"
' Connects with the ODBC driver
r = SQLDriverConnect (hDbc, _
%HWND_DESKTOP, _
szInConnectionString, _
LEN(szInConnectionString), _
szOutConnectionString, _
SIZEOF (szOutConnectionString), _
BYVAL %NULL, _
%SQL_DRIVER_COMPLETE)
' Check for errors
IF ISFALSE SQL_SUCCEEDED(r) THEN
STDOUT SQLGetErrorInfo(%SQL_HANDLE_DBC, hDbc, r)
GOTO Terminate
END IF
' Allocates an statement handle
r = SQLAllocHandle (%SQL_HANDLE_STMT, hDbc, hStmt)
IF ISFALSE SQL_SUCCEEDED(r) OR ISFALSE hStmt THEN GOTO Terminate
' Cursor type
r = SQLSetStmtAttr(hStmt, %SQL_ATTR_CURSOR_TYPE, BYVAL %SQL_CURSOR_KEYSET_DRIVEN, %SQL_IS_UINTEGER)
' Optimistic concurrency
r = SQLSetStmtAttr(hStmt, %SQL_ATTR_CONCURRENCY, BYVAL %SQL_CONCUR_VALUES, BYVAL %SQL_IS_UINTEGER)
' Generates a result set
r = SQLExecDirect (hStmt, "SELECT * FROM Authors ORDER BY Author", %SQL_NTS)
' Check for errors
IF ISFALSE SQL_SUCCEEDED(r) THEN
STDOUT SQLGetErrorInfo(%SQL_HANDLE_STMT, hStmt, r)
GOTO Terminate
END IF
' -------------------------------------------------------------------------------------
' Use SQLDescribeCol to retrieve information about column 2
' -------------------------------------------------------------------------------------
LOCAL szColumnName AS ASCIIZ * 256
LOCAL iNameLength AS INTEGER
LOCAL iDataType AS INTEGER
LOCAL dwColumnSize AS DWORD
LOCAL iDecimalDigits AS INTEGER
LOCAL iNullable AS INTEGER
r = SQLDescribeCol(hStmt, 2, szColumnName, SIZEOF(szColumnName), iNameLength, _
iDataType, dwColumnSize, iDecimalDigits, iNullable)
PRINT "Column name: " & szColumnName
PRINT "Name length: " & FORMAT$(iNameLength)
PRINT "Data type: " & FORMAT$(iDataType)
PRINT "Column size: " & FORMAT$(dwColumnSize)
PRINT "Decimal digits: " & FORMAT$(iDecimalDigits)
PRINT "Nullable: " & FORMAT$(iNullable) & " - " & IIF$(iNullable, "TRUE", "FALSE")
' -------------------------------------------------------------------------------------
Terminate:
' Closes the cursor
IF hStmt THEN SQLCloseCursor(hStmt)
' Frees the statement handle
IF hStmt THEN SQLFreeHandle(%SQL_HANDLE_STMT, hStmt)
' Closes the connection
IF hDbc THEN
SQLDisconnect(hDbc)
SQLFreeHandle (%SQL_HANDLE_DBC, hDbc)
END IF
' Frees the environment handle
IF hEnv THEN SQLFreeHandle(%SQL_HANDLE_ENV, hEnv)
WAITKEY$
END FUNCTION
' ========================================================================================