Solved

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

Posted on 2000-03-29
8
308 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

930 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now