Executing SQL Statements through ADO

Database

Executing SQL Statements through ADO

We execute SQL Statements through ADO in our C++ Applications.

In one of the queries, I need to check if there is any data in the table, and if not drop the table and recreate it based on the user's new definition . If data exists, then the application will not do anything.

Does anybody know how to do this. In SQL Server, we used to use "IF exists" to do this.

4 REPLIES
Enthusiast

Re: Executing SQL Statements through ADO

I am not familiar with C++ ADO application i would suggest you to go with following way:

Check by executing the select statement
SELECT count(*)
FROM XYZ;

IF the count is zero i your C++ application then drop table and recreate it.

Please let me know if this makes sense.

Re: Executing SQL Statements through ADO

Hi

The problem is that I cannot get the result of this query back into C++.

Anybody knows how to get the result of the "Select Count(*)" back into C++ using plain SQL statements. I cannot use Stored procedures etc.
Enthusiast

Re: Executing SQL Statements through ADO

The following method is more efficient than doing a count:

SELECT 1
WHERE EXISTS
(SELECT 1
FROM mytable);

If you get a row back, the table contains rows. If not, the table is empty.

This method stops when it hits the first row rather than continuing through the table (or index when using an index to count the rows).

Enthusiast

Re: Executing SQL Statements through ADO

I hope the below link will help you:

http://www.codeguru.com/cpp/data/mfc_database/ado/article.php/c1141/#more