Solved

How do I use ExecSQL and open adoquery

Posted on 2007-11-26
6
5,190 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SYbase 4 37
Sql Server group by 10 45
How to Get Images From Server using App Tethering 11 40
Syntax for query to update table 2 30
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

807 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