[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

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
0
pucko
Asked:
pucko
  • 5
  • 3
  • 2
  • +2
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now