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
LVL 1
puckoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
 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;
0
Ivanov_GCommented:

  qry2.SQL.Text:= 'sp_helpindex ' + QuotedStr(MyTable.TableOwner + '.' + MyTable.TableName);
  try
    qry2.Open;
    if not qry2.IsEmpty then
       begin
          // your dataset is NOT empty - use it
          // .........
     
       end;
  except
    on E: Exception do
      begin
         MessageDlg(E.Message, mtError, [mbOK], 0);
         Exit;
      end;
  end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
puckoAuthor Commented:
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?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

kretzschmarCommented:
>? but I then it's not so fun to debug
debugging was never funny, i don't see your problem
0
puckoAuthor Commented:
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)
0
puckoAuthor Commented:
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?
0
EKIMCommented:
Pucko : in this case just do nothing in the except clause. Like this the program will not stops on exception
0
kretzschmarCommented:
>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 ;-)
0
puckoAuthor Commented:
How do I disable this behaviour?
0
Ivanov_GCommented:
Tools / Debugger Options / Language Exceptions
and untick "Stop on Delphi Exceptions"
0
DeerBearCommented:
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
0
puckoAuthor Commented:
DeerBear.

Since I get the exeption on "´Qry.Open" this won't help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.