Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-03-29
8
Medium Priority
?
322 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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

772 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