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_S TMT, handle);
}
}
-------------------------- ------
Thanks
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_
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_S
}
}
--------------------------
Thanks
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
For more help, refer SQLTables in MSDN help.
Vicky
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 :)
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()?
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
carldean, exactly what I was looking for.... will use it and see if it works.. :)
ASKER
tq
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