Link to home
Start Free TrialLog in
Avatar of Haho
Haho

asked on

My check table exist function does not work,... any ideas..??

hi guys,

I hv written a check table function that will written a integer code to show if it exists or not using ODBC APIs...
it does not work... always return 0...!
What is wrong?

-----------------------------------

long COdbc::CheckTable (char *szTableName)
{
    SQLCHAR            scName[129];
      SQLHANDLE      handle;

      // Allocate a handle, v3.5 compliant

    SQLRETURN rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &handle);

    if (rc == SQL_SUCCESS)
      {
            rc = SQLTables(handle, NULL, 0, NULL, 0,
                 (SQLCHAR *) szTableName, SQL_NTS, NULL, 0);

            if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
                  return 0; // table do exist
            else
                  return -1; // table do not exist

            // Free the handle
         if (handle != NULL)
            SQLFreeHandle(SQL_HANDLE_STMT, handle);
      }


}

--------------------------------

Thanks
Avatar of V_Bapat
V_Bapat
Flag of India image

I think it is working!!!

This is a part of your code.

if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
return 0; // table do exist

This function returns 0 when SqlTables returns SQL_SUCCESS or SQL_SUCCESS_WITH_INFO. What is the problem?

Vicky
What you may do is that when SQLTables returns SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value may be obtained by calling SQLGetDiagRec with a HandleType of SQL_HANDLE_STMT and a Handle of StatementHandle.

For more help, refer SQLTables in MSDN help.

Vicky
Avatar of Haho
Haho

ASKER

hi guys (and bapat)
 
the problem is that even if the table DOES not exist, it will still return 0!!
If that is the case, I cannot know if the table exists or doesn't exists!! (which is the purpose of the function)

Please reply ASAP.. Thanks :)
Return value 0 means that the call to SQLTables() is successful.
What is the value of szTableName when you call SQLTables()?
You might try a query against the system objects table which will contain the names of existing tables in your database. For example, in SQL Server, you would do something like:

select name from sysobjects where name="yourtablename";


Programmatically you would code that to be carried out by whatever method you are using to execute queries.
ASKER CERTIFIED SOLUTION
Avatar of carldean
carldean
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Haho

ASKER

carldean, exactly what I was looking for.... will use it and see if it works.. :)
Avatar of Haho

ASKER

tq