Solved

How do I use ExecSQL and open adoquery

Posted on 2007-11-26
6
5,106 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2 - Execution Plan 3 57
combine an MS SQL string in Idera DM 9 50
sql query help 4 45
Consolidating oracle query results to a single line 8 53
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now