Solved

How do I use ExecSQL and open adoquery

Posted on 2007-11-26
6
5,299 Views
Last Modified: 2013-11-23
I'm trying the following code


adoquery1.First;

///delete from table all records of  those who have scored under 10 at any time

While not AdoQuery1.eof do begin




If adoquery1['score']<10 then name:=adoquery1['name'];
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('DELETE *');
AdoQuery1.SQL.Add('From Table1');
AdoQuery1.SQL.Add('Where name='+QuotedStr(name));
AdoQuery1.ExecSQL;


Now the problem starts

if I use

adoquery1.Next;


I get the error message Cannot perform this operation on a closed dataset.


If I use

adoquery1.open;
adoquery1.Next;

I get the error message


Current provider does not support returning multiple recordsets from a single execution.







0
Comment
Question by:nicholasjfox
[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
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20349286
The problem with closing a dataset and performing ExecSQL is that it might not return any records, and it doesn't open the database again - it just performs a function.

The proper way to do this is to use 2 Queries.
You can try keep your procedures basically the same as you've typed them above except make one AdoQuery1, and the other AdoQuery2.
0
 
LVL 13

Accepted Solution

by:
rfwoolf earned 250 total points
ID: 20349291

adoquery1.First;
 
While not AdoQuery1.eof do 
begin
  If adoquery1['score']<10 then 
  begin
    name:=adoquery1['name'];
    AdoQuery2.Close;
    AdoQuery2.SQL.Clear;
    AdoQuery2.SQL.Add('DELETE *');
    AdoQuery2.SQL.Add('From Table1');
    AdoQuery2.SQL.Add('Where name='+QuotedStr(name));          
    AdoQuery2.ExecSQL;
  end;
  AdoQuery1.next;
end;

Open in new window

0
 

Author Comment

by:nicholasjfox
ID: 20349305
RTW>

I still get the message 'AdoQuery1: Cannot perform this operation on a closed dataset'
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:nicholasjfox
ID: 20349307
Oh sorry, I didn't see the Making two queries bit. I'll try that.
0
 

Author Comment

by:nicholasjfox
ID: 20349329
Yes, thanks. That's works fine.
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20349354
Cool.
I think the problem there lied in the loop you were trying to perform.
If you go through the loop line by line and imagine what the two ADOQueries (datasets) are doing, you'll see that you were technically asking it to go "next" when you had in fact closed it.
Even if you did re-open it, you would be opening and closing a table in a loop which gets a little bit messy. I still say having two queries is the better way, but if you were interested in getting it to work with only 1 query, you could try something like this:


adoquery1.First;
 
While not AdoQuery1.eof do
begin
  If AdoQuery1['score']<10 then
  begin
    name:=AdoQuery1['name'];
    AdoQuery1.Close;
    AdoQuery1.SQL.Clear;
    AdoQuery1.SQL.Add('DELETE *');
    AdoQuery1.SQL.Add('From Table1');
    AdoQuery1.SQL.Add('Where name='+QuotedStr(name));          
    AdoQuery1.ExecSQL;
    AdoQuery1.SQL.Clear;
    AdoQuery1.SQL.Add('Select * from Table1');
    AdoQuery1.Open;
  end
else AdoQuery1.next;
end;

====

Finally, there may be some SQL wizards out there that might have been able to come up with an SQL execution query that would do all of this with just 1 line of code -- I think it might be possible, but don't forget the SQL used by delphi and ADO etc is still limited in a few ways - it's only an implementation of SQL so it might not have all the utilities to do it. Anways, if you are bored one day, get a book on SQL and see if it covers something like this.
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

624 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