?
Solved

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

Posted on 2000-03-29
8
Medium Priority
?
318 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:Haho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2668670
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
0
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2668682
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
0
 
LVL 1

Author Comment

by:Haho
ID: 2668873
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 :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:V_Bapat
ID: 2668894
Return value 0 means that the call to SQLTables() is successful.
What is the value of szTableName when you call SQLTables()?
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2669537
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.
0
 
LVL 1

Accepted Solution

by:
carldean earned 200 total points
ID: 2672178
Haho,

Providing you've not messed up the parameters to SQLTables it will always return a success state.  What you need to do after your call to SQLTables is perform a SQLFetch to determine whether any results were returned.

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

if (rc != SQL_SUCCESS &&
    rc != SQL_SUCCESS_WITH_INFO) {
   // An error has occurred - still
   // don't know if tables exists though
}
else {
   // No try to fetch results
   //
   rc = SQLFetch(handle);
   if (rc == SQL_NO_DATA_FOUND) {
      // It definately doesn't exist
   }
   else {
      // Yippee I've found it!!!!
   }
}

Hope this helps
0
 
LVL 1

Author Comment

by:Haho
ID: 2679131
carldean, exactly what I was looking for.... will use it and see if it works.. :)
0
 
LVL 1

Author Comment

by:Haho
ID: 2725225
tq
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question