Solved

How do I use ExecSQL and open adoquery

Posted on 2007-11-26
6
5,036 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now