Solved

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

Posted on 2000-03-29
8
311 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
How to know only "File created" with EventLog 3 35
Problem to open Excel file 15 179
Is there a simple front-end menu system. 9 105
Problem to App source 6 75
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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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