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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5510
  • Last Modified:

How do I use ExecSQL and open adoquery

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
nicholasjfox
Asked:
nicholasjfox
  • 3
  • 3
1 Solution
 
rfwoolfCommented:
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
 
rfwoolfCommented:

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
 
nicholasjfoxAuthor Commented:
RTW>

I still get the message 'AdoQuery1: Cannot perform this operation on a closed dataset'
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nicholasjfoxAuthor Commented:
Oh sorry, I didn't see the Making two queries bit. I'll try that.
0
 
nicholasjfoxAuthor Commented:
Yes, thanks. That's works fine.
0
 
rfwoolfCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now