Link to home
Start Free TrialLog in
Avatar of pucko
pucko

asked on

How to handle an empty result set from a query.

I'm creating an application to check the indexes from some tables using the sp_helpindex.

My problem is that if no index are found I got an empty result set.
(The result I get running the Query analyzer is only the text:

"The object does not have any indexes.")

And I get the exeption " commandtext does not return a resultset.

How get I get around this?


   
    qry2.SQL.Text:= 'sp_helpindex ' + QuotedStr(MyTable.TableOwner + '.' + MyTable.TableName);
    qry2.Open;

    while not qry2.Eof do
    begin
Avatar of kretzschmar
kretzschmar
Flag of Germany image

 qry2.SQL.Text:= 'sp_helpindex ' + QuotedStr(MyTable.TableOwner + '.' + MyTable.TableName);
  try
    qry2.Open;

    while not qry2.Eof do
    begin
       ....
    end;
  except
     handle your error
  end;
ASKER CERTIFIED SOLUTION
Avatar of Ivanov_G
Ivanov_G
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pucko
pucko

ASKER

the

if not qry2.IsEmpty then

still gives me this error.

Sure I can create an try except around it, but I then it's not so fun to debug. Isn't it any other way?
>? but I then it's not so fun to debug
debugging was never funny, i don't see your problem
Avatar of pucko

ASKER

I mean:

1. I don't like code that I know will get exceptions.

2. Say that I want to run through the program in debug mode from delphi and do the checks on 500 tables and 300 of them don't have indexes.
 
Then I will get an exception 300 times that will stop the program. And I have to press F8 300 times (when trying to find another problem)
Avatar of pucko

ASKER

It is not possible to run ExcecSQL and then check if there is some result from the Query. And if so, then do an open?
Pucko : in this case just do nothing in the except clause. Like this the program will not stops on exception
>in this case just do nothing in the except clause
not really, because if you debug within the ide, the ide itself will popup an exception-window. but you can disable this bahaviour of the ide.

meikl ;-)
Avatar of pucko

ASKER

How do I disable this behaviour?
Tools / Debugger Options / Language Exceptions
and untick "Stop on Delphi Exceptions"
Hi!

The simplest way I know to check if a dataset is emtpy or not is this:

begin
  MyQuery.Open( 'Select * From MyTable' );
  if Not( MyQuery.bof and MyQuery.Eof ) then
  begin
    // I'm sure the dataset contains data
  end;
end;

HTH,

Andrew
Avatar of pucko

ASKER

DeerBear.

Since I get the exeption on "´Qry.Open" this won't help