How to fetch NULL data into C-ODBC program?

From last two months I’m working on C-ODBC project, despite my expertise in Java. This was I encountered while development.

Problem: C-ODBC API does not provide any function which fetch NULL data without throwing a runtime error.

Solution: Need to provide an indicator, which get set when row data contained NULL for that specific column.

1. Define the long typed variable for holding indication about NULL data:

SQLINTEGER i_first_name, i_last_name, i_age; // i for indicator

2. Passed this indicator variable to SQLBindCol function:
SQLBindCol(hstmt, 1, SQL_C_CHAR, first_name, 100, &i_first_name);
SQLBindCol(hstmt, 2, SQL_C_CHAR, last_name, 100, &i_larst_name);
SQLBindCol(hstmt, 3, SQL_C_CHAR, age, 100, &i_age);

More Information:

3. Check whether we fetched any NULL data into a column by checking,

if(i_first_name == SQL_NULL_DATA)
// I put a “null” string into fist_name variable.
strcpy(fist_name, “null”);

This code copies “null” string into that first name column, else put the value you just fetched.

I Searched/Googled this problem, but nothing found specifically discussing this issue, that’s why I wrote one, If you come across more detailed article regarding this, then please let me know..


Popular Posts